问题描述
我有以下配置。
.config(config);
config.$inject = ["$httpProvider"];
function config($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common["X-Requested-With"];
//Reset headers to avoid OPTIONS request (aka preflight)
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
}
至于我的$resource
.factory('Order', order)
order.$inject = ['$resource', "ApiEndpoint", "UserRecord"];
function order($resource, ApiEndpoint, UserRecord) {
var id = null;
UserRecord.retrieve().then(function (user) {
id = user.user_id;
})
return $resource(ApiEndpoint.url + 'orders.json', {}, {
update: {method: 'PUT', url: ApiEndpoint.url + 'orders.json'}
});
}
现在,当我这样打电话给更新时
var params = { name: "new name" };
Order.update(params, {}, function (resp) {
console.log(resp);
})
我的控制台呈现No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.
作为OPTIONS请求。
如果我运行POST请求,一切都会按预期进行。
当我访问服务器日志时,它会验证我是否正在运行OPTIONS请求。 如何解决此问题以运行PUT请求?
1楼
Ori Drori
1
已采纳
2015-07-30 19:16:56
您正在尝试发出CORS(跨源资源共享)请求,这意味着您尝试连接到非源服务器。 您的来源是为您的应用程序提供服务的服务器。
由于浏览器的安全政策,除GET之外,每个请求都需要进行预检握手(OPTIONS),其中非源服务器描述了允许他来自源X的某人使用的标头和方法。
因此,跳过预检的唯一方法是将要访问的资源放在为应用程序提供服务的同一服务器上。