Php 无法获取从跨站点域发送的输入数据
我使用laravel作为后端,angujarjs作为前端来制作应用程序。前端位于另一台服务器上,因此我必须处理跨域策略。我已启用CORS,因此我可以“发送”post请求Php 无法获取从跨站点域发送的输入数据,php,angularjs,laravel,Php,Angularjs,Laravel,我使用laravel作为后端,angujarjs作为前端来制作应用程序。前端位于另一台服务器上,因此我必须处理跨域策略。我已启用CORS,因此我可以“发送”post请求 问题是,当我试图在laravel中获取Input::all()时,请求被取消。(Chrome网络中的状态显示为“已取消”)。但当我不使用输入时,一切都正常 //laravel class SessionController extends BaseController { protected $entity;
问题是,当我试图在laravel中获取Input::all()时,请求被取消。(Chrome网络中的状态显示为“已取消”)。但当我不使用输入时,一切都正常
//laravel
class SessionController extends BaseController {
protected $entity;
public function __construct(SessionEntity $entity)
{
$this->entity = $entity;
}
public function getLogin()
{
return Response::json('hello')->header('Access-Control-Allow-Origin', '*');
}
public function postLogin()
{
//$data = Input::all();
//return Response::json($data);
// $user = $entity->login($data);
// if($user)
// {
// return Response::json($user);
// } else {
// return Response::json($entity->errors(), 400);
// }
//the code below is OK (able to send response back) , but the code above is not, because I am using Input::all()
$data = array(
"email" => "324234",
"password" => "654321"
);
return Response::json($data);
}
}
//安格拉斯
.controller('LoginController', ['$scope', '$http', function($scope, $http) {
$scope.send = function(credential) {
$http({
method: 'POST',
url: 'http://localhost:8000/api/session/login',
data: credential,
headers: {
'Content-Type': 'application/json; charset=UTF-8'
}
})
.success(function(data, status, headers) {
console.log(data);
console.log(status);
console.log(headers);
});
};
}]);
下面是启用CORS的标题
App::after(function($request, $response)
{
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
$response->headers->set('Access-Control-Max-Age', '1728000');
$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
return $response;
});
我错过了什么???我找到了。我使用名称空间,但不包括“使用输入”。 愚蠢的我
此外,我发现我必须显式地将内容类型设置为“application/json”,以便使用laravel中的Input::all()接收数据,否则我将无法获得数据。对于跨域请求,您必须使用而不是json