1、代码如下
预期想通过调用登录接口,获取登录后的cookie,方便接下来的接口操作。但是刚开始在调用接口时,返回的responseHeader总是
与预期不符。通过抓包得知,调用登录接口返回的状态码是302,所以添加了axios.defaults.maxRedirects = 0 来禁止跟随重定向。
但是获取到的responseHeader依然与预期不符,最后分析得知,302会被axios当做error处理,通过error获取responseHeader与预期相符。
const axios = require("axios");
const querystring = require('querystring');
axios.defaults.maxRedirects = 0;
const data = {
user:'xxx',
password:'xxx',
toUrl=null
}
axios.post('https://www.test.net/user/login',qs.stringify(data))
.catch(function (error) {
console.log(error.response.headers)
})
2、另一种方法
var options = {
method:'post',
url: 'https://www.test.net/user/login',
data:
{ user: 'xxx',
password: 'xxx',
toUrl: null },
maxRedirects:0,
transformRequest: [function (data, headers) {
// 对 data

博客围绕axios处理登录接口展开。调用登录接口时,预期获取登录后cookie,起初返回的responseHeader不符预期。因状态码302,添加禁止重定向代码仍未解决,后发现302被axios当error处理,从error获取的responseHeader才符合预期。还提到axios序列化及数据格式转换方法。
最低0.47元/天 解锁文章
401

被折叠的 条评论
为什么被折叠?



