Javascript 响应中的“Access-Control-Allow-Credentials”标头为“”,必须为“true”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43772830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Access-Control-Allow-Credentials' header in the response is '' which must be 'true'
提问by Akhilesh Kumar
I'm using node, express on backend and angular4 at client side which is giving me following error:
我在客户端使用节点,在后端和 angular4 上表达,这给了我以下错误:
XMLHttpRequest cannot load http://localhost:4876/login/check. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
XMLHttpRequest 无法加载http://localhost:4876/login/check。对预检请求的响应未通过访问控制检查:响应中“Access-Control-Allow-Credentials”标头的值为“”,当请求的凭据模式为“包含”时,该值必须为“真”。因此,不允许访问Origin ' http://localhost:4200'。XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
Api for login/check is implimented as below:
用于登录/检查的 API 实现如下:
router.get('/login/check', (req: any, res: any) => {
let api = new ApiConnection(req, res);
let accessCard: IAccessCard = api.getContent(Auth.ACCESS_CARD_KEY);
if(!Auth.isValid(accessCard))
return api.response.error();
ChatBox.auth.isExpired(accessCard, function (err:any, isExpired: boolean) {
if (err) return api.response.error();
if(!isExpired) {
api.cookie("AccessCard", accessCard);
api.response.success(accessCard);
}
else {
api.response.error();
}
})
});
Where router definition is const router = require('express').Router()
路由器定义在哪里 const router = require('express').Router()
Setting middleware for header and cors is as follows:
设置header和cors中间件如下:
export class Application {
private app:any = express();
constructor() {
this.setCors();
this.setHeaders();
}
public getApp():any {
return this.app;
}
private setCors(){
let whitelist = ['http://localhost:4200','http://localhost:80'];
let corsOptions = {
origin: (origin:any, callback:any)=>{
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
this.app.use(cors(corsOptions));
}
private setHeaders() {
this.app.use(function (req:any, res:any, next: any) {
// Website you wish to allow to connect
//res.setHeader('Access-Control-Allow-Origin', Config.WEB_APP_HOST);
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
}
}
On client side I'm using Api as follows:
在客户端,我使用 Api 如下:
public startSession(callback: (status: boolean, result: any) => void ) {
let self: ChatBox = this;
/**
* @res.result: IAccessCard
*/
this.mApiConnection.get(Api.root+'/login/check', (res: any) => {
if (res.status == ResponseStatus.SUCCESS) {
self.mStorage.storeAccessCard(res.result);
self.loadAccount(res.result);
}
callback(res.status, res.result);
})
}
采纳答案by Akhilesh Kumar
While setting cors in corsOptions I added value credentials true it worked as follows:
在 corsOptions 中设置 cors 时,我添加了 value credentials true 它的工作方式如下:
private setCors(){
let whitelist = ['http://localhost:4200','http://localhost:80'];
let corsOptions = {
origin: (origin:any, callback:any)=>{
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},credentials: true
}
this.app.use(cors(corsOptions));
}

