【发布时间】:2015-09-19 23:49:42
【问题描述】:
我刚刚通过barryvdh 安装了 Laravel CORS,据我所知,该模块运行良好,但我似乎仍然面临 Access-Control-Allow-Origin 错误
XMLHttpRequest 无法加载 http://acns.example.com:8000/status/d6uIlvwwi8PrvQe4kQfufyZ0LlqQqJyGeyJjdC…I4OTYzMTJlYzYyMmMxOTVkNWI5YjNjYzM1MTczNyIsInMiOiI2ZDcwZDg5N2FkOTQxZDFkIn0=。 请求中不存在“Access-Control-Allow-Origin”标头 资源。原点 'http://www.example.com:8000' 因此不是 允许访问。
下面是我执行该功能的角度代码:
var SomeApp = angular.module('SomeApp',['ngResource','ngSanitize'])
SomeApp.factory('SomeAppService', ['$resource', function($resource){
return {
firstActions : $resource(svrid('acns') + '/action/:payload',{payload:'@payload'},
{
'remove': {method:'DELETE',isArray:true, cache:false},
'save' : {method:'POST', isArray:true, cache:false},
'query' : {method:'GET', isArray:true,cache:false},
}, {cache:false}
),
//some more other functions
};
}]);
在深入研究代码时,我意识到假设附加的标头未包含在 xhr 请求中(请参阅下图)
我在这里缺少什么?
更新 1: 我稍微缩小了最可能与使用 asm89's stack-cors 的 barryvdh's laravel-cors 相关的问题,其中 config\cors.php 未正确传递给 asm89。对这个问题不是很自信,但我做了一些手动覆盖,导致OPTIONS 在我手动将config\cors.php 中的数组传递给asm89 时工作,但另一方面又导致其他方法失败。
更新 2: 我尝试手动更改 Asm89\Stack\CorsService 下的部分,给它一个默认值,如下所示:
private function normalizeOptions(array $options = array())
{
$options += array(
'allowedOrigins' => array('*'),
'supportsCredentials' => true,
'allowedHeaders' => array('*'),
'exposedHeaders' => array('*'),
'allowedMethods' => array('*'),
'maxAge' => 0,
);
// Some other codes
// normalize array('*') to true
return $options;
}
并注释掉一小部分
public function handlePreflightRequest(Request $request)
{
/*if (true !== $check = $this->checkPreflightRequestConditions($request)) {
return $check;
}*/
return $this->buildPreflightCheckResponse($request);
}
它非常适合预检 OPTIONS 和 GET 方法,但 POST 和 DELETE 方法会提示我错误
请求中没有“Access-Control-Allow-Origin”标头 资源。原点 'http://www.example.com:8000' 因此不是 允许访问。响应的 HTTP 状态代码为 500。
预检后OPTIONS请求
【问题讨论】:
-
如果我没记错的话,Access-Control-Allow-Origin 标头应该出现在您的响应中,因此您可能需要更新提供响应的服务器。
-
@Shilly 你是怎么做的?
标签: javascript angularjs laravel laravel-5