CORS:當憑據標志為真時,無法在訪問控制允許原產地中使用通配符我有個計劃前端服務器(Node.js,域:localhost:3000)<->后端(Django,Ajax,Domain:localhost:8000)Browser<-webapp<-Node.js(為應用程序服務)Browser(Webapp)->ajax->Django(服務Ajax POST請求)現在,我的問題是使用CORS設置,Webapp用來對后端服務器進行Ajax調用。在鉻,我不斷得到當憑據標志為真時,無法在訪問控制-允許-原產地中使用通配符?;鸷膊黄鹱饔谩N业腘ode.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/)
CORS:當憑據標志為真時,無法在訪問控制允許原產地中使用通配符
德瑪西亞99
2019-06-20 17:16:38