我有一个涉及的设置
前端服务器(Node.js,domain:localhost:3000)<--->后端(Django,Ajax,域:localhost:8000)
浏览器< - webapp < - Node.js(服务应用)
浏览器(webapp) - > Ajax - > Django(服务ajax POST请求)
现在,我的问题在于CORS设置,webapp使用它来向后端服务器进行Ajax调用.在chrome中,我一直在努力
当credentials标志为true时,无法在Access-Control-Allow-Origin中使用通配符.
在Firefox上也不起作用.
我的Node.js设置是:
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:8000/');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
};
Run Code Online (Sandbox Code Playgroud)
webapp发出如下请求:
$.ajax({
type: "POST",
url: 'http://localhost:8000/blah',
data: {},
xhrFields: {
withCredentials: true
},
crossDomain: true,
dataType: 'json',
success: successHandler
});
Run Code Online (Sandbox Code Playgroud)
因此,webapp发送的请求标头如下所示:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"
Access-Control-Allow-Methods: 'GET,PUT,POST,DELETE'
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: …Run Code Online (Sandbox Code Playgroud) 是的,我知道你在想什么 - 还有另一个CORS问题,但这次我很难过.
所以要开始,实际的错误信息:
XMLHttpRequest无法加载http://localhost/Foo.API/token.当请求的凭据模式为"include"时,响应中"Access-Control-Allow-Origin"标头的值不能是通配符"*".因此不允许来源' http:// localhost:5000 '访问.XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制.
我不确定凭证模式的含义是'包含'?
所以当我在邮递员中执行请求时,我没有遇到这样的错误:
但是当我通过我的angularjs网络应用程序访问相同的请求时,我被这个错误所困扰.这是我的angualrjs请求/回复.你会看到响应OK 200,但我仍然收到CORS错误:
小提琴请求和响应:
下图演示了Web前端到API的请求和响应
所以基于我在网上阅读的所有其他帖子,看起来我做的是正确的,这就是为什么我无法理解错误.最后,这是我在angualrjs(登录工厂)中使用的代码:
API中的CORS实现 - 参考目的:
方法1使用:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
EnableCrossSiteRequests(config);
}
private static void EnableCrossSiteRequests(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*")
{
SupportsCredentials = true
};
config.EnableCors(cors);
}
}
Run Code Online (Sandbox Code Playgroud)
方法2使用:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration(); …Run Code Online (Sandbox Code Playgroud) 突然间,似乎没有改变我的网络应用程序中的任何内容,我在Chrome中打开它时开始出现CORS错误.我尝试添加Access-Control-Allow-Origin: *标题.然后我收到这个错误:
XMLHttpRequest cannot load http://localhost:9091/sockjs-node/info?t= 1449187563637. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:3010' is therefore not allowed access.
但正如您在下图中看到的那样,没有Access-Control-Allow-Credentials标题.
WTF?Chrome bug?
我的页面被加载,http://localhost:3010该服务器也使用Access-Control-Allow-Origin: *没有问题.如果两个端点都使用它会有问题吗?