【发布时间】:2016-12-31 18:39:23
【问题描述】:
我有一个角度应用程序,我将数据(通过 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:
有人知道错误可能是什么或我做错了什么吗?
【问题讨论】:
-
那是因为你的路线是一个get。
Route::get你正在尝试发帖。 -
我认为您需要检查错误日志或查看开发人员工具中的响应结果以获取有关错误的更多信息。如果您在浏览器中调用 post 路由,则会收到错误,因为浏览器正在通过 get 请求.. 所以 post 应该没问题或同时定义两者。
-
将 this.http.post 更改为 this.http.get 或其他内容(如果可用)或更改您的服务器路由类型,因为 post 路由器将其从 csrf 验证中间件中排除
-
我怎样才能排除它?以前没做过,