我有一个角度应用程序,在这里我通过json将数据发送到我的laravel服务器。我的服务器在VM(ubuntu)上:
这是我从angular应用程序将其发送到服务器的地方。
this.http.post(this.loginURL, requestBody, options)
在我的laravel服务器上,我有以下路线:
Route::get('patientlogin','UploadController@login');
和控制器的方法
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();
}
现在,当我运行该应用程序时,我得到了错误:
POST http:// ip / patientlogin 405(不允许使用方法)
当我更改web.php中的get来发布时,出现此错误:
polyfills.js:1 POST http://ip/patientlogin 500 (Internal Server Error)
当我尝试在浏览器中调用网址时:
MethodNotAllowedHttpException in RouteCollection.php line 218:
有人知道错误可能是什么或我在做什么错吗?
HTTP GET不能具有主体(从技术上讲,它可以,但并非必须如此)。因此,如果要在请求中发送正文,则应使用post或put。
您获取的方法不允许,因为您将路由配置为使用GET not POST。
改变
Route::get
至
Route::post
那应该解决
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句