javascript - node js 从 302 重定向获取 cookie

标签 javascript node.js http request http-headers

我想用node js模拟登录。请求为post请求,返回302。

当我想用 Node 中的请求模拟它时,我得到错误:

Unhandled rejection StatusCodeError: 302 

但是当我查看谷歌浏览器时,响应是空的,但有一个 Cookie 响应。我如何通过 Node 请求获取此 cookie?

enter image description here

最佳答案

获取cookies的示例代码如下:

$.ajax({
    type: "POST",
    url: AppConstants.URLs.PROXY,
    data: message,
    xhrFields: {
        withCredentials: true
    },
    success: function(data, status, xhr) {
        console.log("Cookie: " + xhr.getResponseHeader("Set-Cookie"));
    }
});

您可以使用 Cookies-js 在 node.js 的服务器端设置 cookie

How can i get this cookie with a node request?

var jsdom = require('jsdom');
var window = jsdom.jsdom().parentWindow;
var Cookies = require('cookies-js')(window);

// First set a cookie
Cookies.set('key', 'value');

// Get the cookie value
Cookies.get('key'); // "value"

// Using the alias
Cookies('key'); // "value"

希望这对您有所帮助。

关于javascript - node js 从 302 重定向获取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51495690/

相关文章:

当 <!doctype html> 以正确的格式插入时,Javascript 不工作

javascript - 如何防止文本区域的事件冒泡,同时保持其可编辑性?

javascript - 动态构造包/目录中所有对象的列表

node.js - Express.js 继续以文本形式提供 Angular2 index.html

javascript - Node.JS,服务器在请求过程中关闭 Keep-Alive 连接

javascript - 最紧凑的 JSON 数据 URL 编码?

javascript - 使用 Angular 在 NVD3 图表上实时更改轴标签

javascript - 使用 Javascript 进行响应式编程

ios - NSURLRequest 什么时候超时?

python - 如何转储http流量?