我正在尝试为跨域请求设置一些标头。
Http客户端:http://localhost:8080 Http Server:http://localhost:8081
这是我的Http Server 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
时,一切正常。
知道我可能做错了吗?
提前感谢您的帮助。
答案 0 :(得分:1)
添加请求标头时,浏览器没有理由阻止X域请求。 您应该使用Access-Control-Allow-Origin标头检查您的服务器是否仍在响应。
答案 1 :(得分:0)
问题来自NancyFx,我不确定它现在是否已修复。
您可以在此处了解详情:https://github.com/NancyFx/Nancy/issues/1947。