vue 前端解决跨域问题,

####

vue客户端发送两次请求的解决办法

第一种:
设置axios的Content-Type:
axios.defaults.headers[‘Content-Type’] = ‘application/x-www-form-urlencoded’

header中content-type 设为 application/x-www-form-urlencoded;
header中不要设置其他参数。

###

npm install qs --save

因此,我在该模块中引入 qs
import qs from 'qs'

然后定义一下默认的 Content-Type
service.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

最后,在request拦截器里面判断一下,如果是当前请求是post,就执行qs.stringify()

这样一来,在跨域的情况下发送出去的就是一个常规的 URLSearchParams 数据

####

原文地址:https://www.cnblogs.com/andy0816/p/15558063.html