![](/img/trans.png)
[英]How to disable 'withCredentials' in api call using node.js only (https package)
[英]How to disable 'withcredentials' in HTTP header with node.js and Request package?
使用node.js和浏览器中的Request包(通过browserify ),我使用CORS在单独的域上执行HTTP GET请求。
在服务器上,当我将'Access-Control-Allow-Origin'
设置为通配符'*'
,我在客户端上收到以下错误:
XMLHttpRequest无法加载....当credentials标志为true时,不能在'Access-Control-Allow-Origin'标头中使用通配符'*'。 因此不允许原点'...'访问。
HTTP请求标头如下所示:
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ja;q=0.6
Access-Control-Request-Headers:withcredentials
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:3000
Origin:http://localhost:9966
Pragma:no-cache
Referer:http://localhost:9966/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
很明显问题是Access-Control-Request-Headers:withcredentials
中有Access-Control-Request-Headers:withcredentials
,对吧?
为了能够删除它,我需要将'XMLHttpRequest'对象的'withcredentials'属性设置为'false'。 但是,我无法弄清楚node.js或Request包在哪里创建'XMLHttpRequest'对象,以及我如何访问它。
谢谢。
经过一些调查,我发现withCredentials
设置可以通过options参数对象传递:
var req = http.request({
withCredentials: false
}, function(res) {
//...
});
req.end();
如果undefined
,则默认设置为true
。
来自http-browserify/lib/request.js
源代码的引用:
if (typeof params.withCredentials === 'undefined') {
params.withCredentials = true;
}
try { xhr.withCredentials = params.withCredentials }
catch (e) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.