【发布时间】:2015-08-10 07:04:40
【问题描述】:
我正在尝试为跨域请求设置一些标头。
Http 客户端:http://localhost:8080 Http 服务器:http://localhost:8081
这是我的 Http 服务器 CORS 配置:
"Access-Control-Allow-Origin" : "http://localhost:8080"
"Access-Control-Allow-Methods" : "POST,GET,DELETE,PUT,OPTIONS"
"Access-Control-Allow-Credentials" : "true"
"Access-Control-Allow-Headers" : "Accept,Origin,Content-type,MY_HEADER"
"Access-Control-Expose-Headers" : "Accept,Origin,Content-type,MY_HEADER"
在我向请求中添加自定义标头之前,任何查询都可以正常工作。 当我这样做时,我收到以下错误:
XMLHttpRequest cannot load http://localhost:8081/auth. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
这是我添加自定义标头的 ajax 代码:
$.ajax({
beforeSend : function(xhr) {
xhr.setRequestHeader('MY_HEADER', 'my value');
},
crossDomain : true,
xhrFields: {
withCredentials: true
}
method: httpMethod,
url: url,
data: data,
dataType: 'json',
accept: {
json: 'application/json'
},
async: true,
});
当我删除 beforeSend 时,一切正常。
知道我做错了什么吗?
提前感谢您的帮助。
【问题讨论】:
标签: ajax cross-domain cors