我想用node js模拟登录。请求为post请求,返回302。
当我想用 Node 中的请求模拟它时,我得到错误:
Unhandled rejection StatusCodeError: 302
但是当我查看谷歌浏览器时,响应是空的,但有一个 Cookie 响应。我如何通过 Node 请求获取此 cookie?
最佳答案
获取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/