六个小时过去了,我仍然没有得到以下问题的解决方案。
我正在尝试让 AngularJS 从不同的域访问我的 API。在网上搜索后我发现了这个package它说它可以“在 Laravel 应用程序中添加 CORS(跨源资源共享)标头支持”
我遵循了所有的指示。设置这个和那个让它工作,但仍然没有运气。我的服务器总是向我发送相同的以下错误:
XMLHttpRequest 无法加载。请求的资源上不存在“Access-Control-Allow-Origin”标头。起源 'http://lab.angularapi' 因此不允许访问。
这是我的角度代码:
var Demo = angular.module( "Demo", [ "ngResource" ] );
Demo.controller(
"ListController"
function( $scope, ,$http, $resource ) {
$http.defaults.useXDomain = true;
$scope.useResource = function() {
var Lists = $resource('http://lab.laracon/v1/lists', {
username: 'OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3',
password: 'whatever'
});
Lists.get({
id: 1
}, function(data) {
alert(data.ok);
});
};
}
);
这是我的 barryvdh laravel-cors 配置文件:
'defaults' => array(
'allow_credentials' => false,
'allow_origin' => array(),
'allow_headers' => array(),
'allow_methods' => array(),
'expose_headers' => array(),
'max_age' => 0,
),
'paths' => array(
'^/v1/' => array(
'allow_origin' => array('*'),
// 'allow_headers' => array('Content-Type'),
'allow_headers' => array('*'),
'allow_methods' => array('POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'),
'max_age' => 3600,
),
),
最后这是我的 nginx 服务器配置:
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
add_header 'Access-Control-Allow-Origin' 'http://lab.angularapi';
add_header 'Access-Control-Allow-Credentials' 'false';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
}
谁能帮我?我的代码和配置有什么问题?谢谢