本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
项目配置
- vue-cli3
- vue2
- element-ui-2.15.3
在网上找了很多方案,但是都没有生效,最后是前端添加了一段代码,后台添加了一段代码 解决了
添加了headers的配置
const instance = axios.create({
// baseURL: 'http://mall.huolida.com/',
// baseURL: '/api',
withCredentials: false, // 允许携带cookie
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
},
baseURL: '/api',
changeOrigin: true,//是否跨域
timeout: 100000
})
添加了header的配置
header('Access-Control-Allow-Origin: http://192.168.1.117:8080');
// 也可以添加所有的请求为 允许,当然不推荐这样做了、
header('Access-Control-Allow-Origin: *');
这样就解决了困扰我好久的跨域
参考链接:[【Web前端问题】vue axios 配置上服务器后报错'Access-Control-Allow-Origin' header] - H5W3](www.h5w3.com/144032.html "【Web前端问题】vue axios 配置上服务器后报错'Access-Control-Allow-Origin' header] - H5W3")
参考链接:vue axios请求出错No 'Access-Control-Allow-Origin' header is present on the requested resource - 简书