laravel Angular $http 返回“no access control allow origin”标头,但 jQuery $.get 请求返回信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24743598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular $http returns "no access control allow origin" header, but jQuery $.get request returns information
提问by Andrew Allbright
How the heck can jQuery $.get, $.post and get desired results but angular can't $http.get, $http.post? Why is the origin policy not working for angular but for jquery?
jQuery $.get, $.post 怎么能得到想要的结果而angular 不能$http.get, $http.post?为什么原始策略不适用于 angular 而是适用于 jquery?
Laravel backend, angular front-end. I'm considering just using jQuery because it doesn't prevent CRUD actions from happening on the client-side.
Laravel 后端,角度前端。我正在考虑只使用 jQuery,因为它不会阻止 CRUD 操作在客户端发生。
I configured $http and $httpProvider...
我配置了 $http 和 $httpProvider ...
.run(function($http){
$http.defaults.headers.common['Access-Control-Allow-Origin'] = "*";
$http.defaults.headers.common['Access-Control-Allow-Methods'] = "GET, POST, PUT, DELETE, OPTIONS";
$http.defaults.headers.common['Access-Control-Allow-Headers'] = "Authorization";
})
.config(function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
})
And laravel is sending back the appropriate header...
而laravel 正在发回适当的标头...
App::after(function($request, $response)
{
// header('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Origin', '*');
});
So the weird thing is angular $http cannot get anything back from the server and produces this error:
所以奇怪的是 angular $http 无法从服务器获取任何东西并产生这个错误:
XMLHttpRequest cannot load http://0.0.0.0:8000/api/test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
But jQuery $.get and $.post work correctly!
但是 jQuery $.get 和 $.post 工作正常!
$.post('http://0.0.0.0:8000/api/test', function(resp){
console.log(resp);
});
What am I doing wrong here?
我在这里做错了什么?
回答by dave
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);
Just setting useXDomain
to true is not enough. AJAX request are also send with the X-Requested-With
header, which indicate them as being AJAX. Removing the header is necessary, so the server is not rejecting the incoming request.
仅仅设置useXDomain
为 true 是不够的。AJAX 请求也与X-Requested-With
标头一起发送,表明它们是 AJAX。删除标头是必要的,因此服务器不会拒绝传入的请求。
Try adding this within filters.php:
尝试在 filters.php 中添加它:
App::before(function($request)
{
if (Request::getMethod() == "OPTIONS") {
$headers = array(
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Headers'=> 'X-Requested-With, content-type, Authorization',);
return Response::make('', 200, $headers);
}
});
It might be failing on the pre-flight request
飞行前请求可能失败