使用node.js和浏览器中的Request包(通过browserify),我使用CORS在单独的域上执行HTTP GET请求.
在服务器上,当我设置'Access-Control-Allow-Origin'
为通配符时'*'
,我在客户端上收到以下错误:
的XMLHttpRequest无法加载....通配符"*"不能在使用"访问控制允许来源"标题时,凭证标记为真.因此不允许原点'...'访问.
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
在标题中,对吗?
为了能够删除它,我需要将'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) {}