可以尝试在请求中添加“followRedirects:true”选项,代码示例如下:
axios.get(url, { followRedirects: true }) .then(response => { console.log(response); }) .catch(error => { console.log(error); });
另外,如果请求的url是https协议,需要添加“httpsAgent”选项并设置“rejectUnauthorized”为false,代码示例如下:
const https = require('https'); const agent = new https.Agent({ rejectUnauthorized: false });
axios.get(url, { httpsAgent: agent, followRedirects: true }) .then(response => { console.log(response); }) .catch(error => { console.log(error); });
通过以上方法,可以解决axios在重定向时返回406错误码的问题。