Ich habe eine eckige App, wo ich Daten (über Json) an meinen Laravel-Server sende. Mein Server ist auf einer VM (ubuntu):JSON-Daten an Laravel-Server senden
Dies ist, wo ich es aus der eckigen App an den Server senden.
this.http.post(this.loginURL, requestBody, options)
Auf meinem Laravel Server habe ich die Route:
Route::get('patientlogin','[email protected]');
Und die Controller-Methode
public function login(Request $request){
// error_reporting(-1); // prints every error, warning, etc
error_reporting(0); // no output at all
// set content-type of response to json
header('Content-Type: application/json');
// import Auth class and custom functions
// require_once('custom_functions.php');
$LOGIN_LOG_FILE = "login1.log";
$AUTH_HEADERS_FILE = "auth-headers1.txt";
/*
php://input is raw input, regardless of header field "content-type"
The PHP superglobal $_POST, only is supposed to wrap data that is either
application/x-www-form-urlencoded or multipart/form-data-encoded
http://stackoverflow.com/a/8893792
When sending only a JSON, $_POST etc will not be populated and php://input has to be used
in the php scripts
http://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission
http://php.net/manual/de/wrappers.php.php
*/
$content = $request->instance();
$json_raw = $content->json()->all();
$json = json_decode($json_raw, true);
/* <-- DEBUGGING START TODO delete */
//read the header, where username and password are supposed to be in
$headers = apache_request_headers();
//print the contents of the headers array in a neat structure and log them
$headersPrintable = print_r($headers, true);
file_put_contents($AUTH_HEADERS_FILE, $headersPrintable, FILE_APPEND);
$request = print_r($_REQUEST, true);
$post = print_r($_POST, true);
file_put_contents("auth-req.txt", $request, FILE_APPEND);
file_put_contents("auth-post.txt", $post, FILE_APPEND);
file_put_contents("auth-req-json.txt", $json_raw, FILE_APPEND);
file_put_contents("auth-req-json_decoded.txt", $json, FILE_APPEND);
/* DEBUGGING END --> */
$valid = false;
$username = "";
//check if username and passord exist in the json-decoded version of php://input
if(array_key_exists("username", $json) && array_key_exists("password", $json)) {
$username = $json["username"];
$password = $json["password"];
$valid = Auth::checkCredentials($username, $password);
}
$response = array(
"username" => $username,
"valid" => $valid
);
echo json_encode($response);
//exit();
}
Nun, wenn ich die App betreibe ich den Fehler haben:
POST http://ip/patientlogin 405 (Methode nicht erlaubt)
Als ich die get in meinem web.php wechsle ich zu schreiben bekommen diesen Fehler:
polyfills.js:1 POST http://ip/patientlogin 500 (Internal Server Error)
und wenn ich versuche, die URL im Browser aufrufen:
MethodNotAllowedHttpException in RouteCollection.php line 218:
Jemand eine Idee Was könnte der Fehler sein oder was mache ich falsch?
lösen das ist, weil Sie Ihre Route ein Get ist. 'Route :: get' und du versuchst zu posten. – Erevald
Ich denke, Sie müssen Ihre Fehlerprotokolle überprüfen oder das Ergebnis der Reaktion in Ihren Entwicklertools nachsehen, um weitere Informationen zu dem Fehler zu erhalten. Wenn Sie die Postroute im Browser aufrufen, erhalten Sie einen Fehler, da der Browser über get angefordert wird. Post sollte also in Ordnung sein oder beide definieren. –
ändern this.http.post to this.http.get oder etwas, wenn verfügbar oder ändern Sie Ihre Server Routentyp als post-Router es aus csrf verify Middleware –