发送json数据到Laravel服务器
我有一个角度的应用程序,我发送数据(通过json)到我的laravel服务器。 我的服务器在虚拟机上(ubuntu):发送json数据到Laravel服务器
这是我从角度应用程序发送给服务器的地方。
this.http.post(this.loginURL, requestBody, options)
在我laravel服务器我的路线:
Route::get('patientlogin','[email protected]');
而且控制方法
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后我得到这个错误:
polyfills.js:1 POST http://ip/patientlogin 500 (Internal Server Error)
,当我尝试调用的URL在浏览器:
MethodNotAllowedHttpException in RouteCollection.php line 218:
有人什么想法错误可能是什么或我做错了什么?
HTTP GET不能有一个正文(它可以,在技术上,但它不是)。所以如果你在请求中发送一个正文,你应该使用post或put。
由于您将路由配置为使用GET而不是POST,因此您将获得不允许的方法。
变化
Route::get
要
Route::post
这应该解决它
获取请求没有正文?大声笑 –
Emmmm是否错了?为什么哈哈? –
GET只是http方法的一个动词。每种方法都可以有一个身体。 –
那是因为你的路线是一个GET。 'Route :: get',你正在尝试发布。 – Erevald
我认为您需要检查错误日志或查看开发人员工具中的响应结果以获取有关错误的更多信息。如果你在浏览器中调用post路径,你会得到一个错误,因为浏览器正在通过get ..来请求,所以post应该没问题或者定义两者。 –
将this.http.post更改为this.http.get或某些东西(如果可用)或更改您的服务器路由类型为post路由器将其从csrf中排除它验证中间件 –