本文介绍了如何使用node.js和Request包禁用HTTP头中的“withcredentials”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从浏览器中使用node.js和软件包(通过),我正在使用CORS在单独的域上执行HTTP GET请求。

Using node.js and the Request package from the browser (via browserify), I am using CORS to do a HTTP GET request on a separate domain.

在服务器上,当我设置<$ c时$ c>'Access-Control-Allow-Origin'到通配符'*',我在客户端上收到以下错误:

On the server, when I set 'Access-Control-Allow-Origin' to the wildcard '*', I get the following error on the client:

的XMLHttpRequest不能加载....通配符 '*' 不能在$ b $使用b '访问控制允许来源'凭证标志为
时为标头。因此,不允许访问...。

HTTP请求标头如下所示:

The HTTP request header looks like this:

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'对象,以及我如何访问它。

To be able to remove this, I need to set the 'withcredentials' property of the 'XMLHttpRequest' object to 'false'. However, I cannot figure out where node.js or the Request package are creating the 'XMLHttpRequest' object, and how I can even access this.

谢谢。

推荐答案

经过一番调查后,我发现 withCredentials 设置可以通过options参数对象:

After some investigation, I discovered that the withCredentials setting can be passed in via the options parameter object:

var req = http.request({
    withCredentials: false
}, function(res) {
    //...
});

req.end();

如果未定义,则默认设置为 true

来自 http-browserify / lib / request.js的参考来源:

if (typeof params.withCredentials === 'undefined') {
    params.withCredentials = true;
}

try { xhr.withCredentials = params.withCredentials }
catch (e) {}

这篇关于如何使用node.js和Request包禁用HTTP头中的“withcredentials”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:52
查看更多