在使用axios时不能与其他http(s).Agent模块一起使用,需要修改代码,使用默认的http(s).Agent模块。以下是示例代码:
const axios = require('axios'); const axiosCookieJarSupport = require('axios-cookiejar-support').default; const tough = require('tough-cookie'); const cookieJar = new tough.CookieJar();
axiosCookieJarSupport(axios);
axios.defaults.jar = cookieJar; axios.defaults.withCredentials = true; axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.get('https://example.com').then(response => { console.log(response.data); }).catch(error => { console.error(error); });
修改后的代码中使用了默认的http(s).Agent模块,不再使用其他http(s).Agent模块,可以成功运行axios-cookiejar-support模块。