CORS:当凭据标志为真时,无法在访问控制允许原产地中使用通配符我有个计划前端服务器(Node.js,域:localhost:3000)<->后端(Django,Ajax,Domain:localhost:8000)Browser<-webapp<-Node.js(为应用程序服务)Browser(Webapp)->ajax->Django(服务Ajax POST请求)现在,我的问题是使用CORS设置,Webapp用来对后端服务器进行Ajax调用。在铬,我不断得到当凭据标志为真时,无法在访问控制-允许-原产地中使用通配符。火狐也不起作用。我的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();};在Django,我用这个中间件 还有这个Web应用程序以这样的方式提出请求:$.ajax({
type: "POST",
url: 'http://localhost:8000/blah',
data: {},
xhrFields: {
withCredentials: true
},
crossDomain: true,
dataType: 'json',
success: successHandler});因此,Web应用程序发送的请求头如下所示:Access-Control-Allow-Credentials: trueAccess-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: en-US,en;q=0.8
Cookie: csrftoken=***; sessionid="***"下面是响应头:Access-Control-Allow-Headers: Content-Type,*Access-Control-Allow-Credentials: trueAccess-Control-Allow-Origin:
*Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETEContent-Type: application/json我哪里出问题了?!编辑1:我一直在使用chrome --disable-web-security,但现在想让事情真正发挥作用。编辑2:回答:所以,我的解决方案django-cors-headers配置:CORS_ORIGIN_ALLOW_ALL = FalseCORS_ALLOW_CREDENTIALS = TrueCORS_ORIGIN_WHITELIST = (
'http://localhost:3000' # Here was the problem indeed and it has to be http://localhost:3000, not http://localhost:3000/)
3 回答
不负相思意
TA贡献1777条经验 获得超10个赞
withCredential
var cors = require('cors'); app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
添加回答
举报
0/150
提交
取消