![]() |
活泼的饼干 · javascript标签调用方法 ...· 1 年前 · |
![]() |
爱笑的回锅肉 · 前沿资讯 - 程序员 | C++ 全栈知识体系· 1 年前 · |
![]() |
勤奋的充电器 · 获取硬件UUID方法(windows、lin ...· 1 年前 · |
![]() |
发呆的茴香 · 最小的SciPy Docker文件· 1 年前 · |
![]() |
强健的豆浆 · MySQL中group_concat()函数 ...· 1 年前 · |
我有一个设置,包括
前端服务器(Node.js,domain: localhost:3000) <->后端(Django,Ajax,domain: localhost:8000)
浏览器<-- webapp <-- Node.js (为应用程序提供服务)
浏览器(webapp) --> Ajax --> Django(服务ajax POST请求)
现在,我这里的问题是CORS设置,webapp使用它来对后端服务器进行Ajax调用。在chrome中,我不断地得到
当凭证标志为true时,
不能在访问控制允许源中使用通配符。
在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();
};
在姜戈,我使用的是 this middleware along with this
webapp发出的请求如下:
$.ajax({
type: "POST",
url: 'http://localhost:8000/blah',
data: {},
xhrFields: {
withCredentials: true
crossDomain: true,
dataType: 'json',
success: successHandler
});
因此,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: en-US,en;q=0.8
Cookie: csrftoken=***; sessionid="***"
下面是响应头:
Access-Control-Allow-Headers: Content-Type,*
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Content-Type: application/json
我哪里错了?!
编辑1:我一直在使用
chrome --disable-web-security
,但现在我希望它能正常工作。
编辑2:答案:
因此,针对我的
django-cors-headers
配置的解决方案:
CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
'http://localhost:3000' # Here was the problem indeed and it has to be http://localhost:3000, not http://localhost:3000/
)
发布于 2013-11-03 01:31:57
这是安全的一部分,你不能那样做。如果您希望允许凭据,则您的
Access-Control-Allow-Origin
不能使用
*
。您必须指定确切的协议+域+端口。有关参考,请参阅以下问题:
此外,
*
过于宽松,会使凭据的使用失效。因此将
http://localhost:3000
或
http://localhost:8000
设置为allow origin header。
发布于 2017-08-26 08:30:16
如果您正在使用CORS中间件,并且您想要发送
withCredential
布尔值true,您可以这样配置CORS:
var cors = require('cors');