axios.post发送请求一直报错403,一看请求方式变成了option,反正就各种报错吧,
昨天加班也没搞定,今天用postman测试了一下,postman正常显示。
心想还好没去问老大,不然可能就尴尬了。。
然后看到一篇文章
******************************************************************************
上面的结果导致总是返回404 ,因为后台不允许options访问。
后来查询各种资料发现:根源在于,我们发出去的请求不是 simple request,那么在每次发送请求之前,都会发送一个options请求,simple request 需要同时满足以下条件(规范可以百度查询):
get、post、head 请求类型
不要设置列表之外的header(如: user-agent)
Content-Type 只能是:
application/x-www-from-urlencoded
multipart/from-data
text/plain
***********************************************************************************
又想到昨天用全局配置post方法的data数据的qs.stringify
api.interceptors.request.use(function (config) {
if(config.method === 'post'){
config.data = qs.stringify(config.data)
}
于是在这里又加了一条
config.headers = {'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'}
然后就可以了
但是。。。
之前已经有这一条了
const api= axios.create();
api.defaults.baseURL = 'http://192.168.1.101:8080/mall/';
api.defaults.timeout = 5000;
api.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
api.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'
那之前的这一条为什么不生效呢???
待解答