当前位置: 移动技术网 > IT编程>开发语言>JavaScript > VUE axios 跨域问题 No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

VUE axios 跨域问题 No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

2020年07月08日  | 移动技术网IT编程  | 我要评论

开发环境中,使用axios调用接口时,出现跨域的时候会被浏览器拦截,故而造成请求失败,并且在控制台中显示 No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8081’ is therefore not allowed access.**

第一步:看看你的项目根部有没有vue.config.js这个文件;
如果没有那你要用“终端”输入“vue ui” 打开vue项目管理界面,找到下面这个配置项,配置好点击保存就在对应的路径生成vue.config.js文件了:
在这里插入图片描述
打开vue.config.js 文件,配置需要代理的请求路径前缀 http://www.xxxxxxxx.xxx 或者http://xxx.xxx.xx.xx:xxxx:
在这里插入图片描述
配置好之后,记得重启vue项目,要不是不会生效的。

请求前缀,你可以统一在这里设置,也可以不在这里设置,在请求的时候加上。
在这里插入图片描述

axios.get('/api/前后端约定的接口路径').then(res=>{
  console.log(res)
}).catch(err=>{
  console.log(err)
})

弄好后,启动项目就可以了。

本文地址:https://blog.csdn.net/lelesidai/article/details/107185509

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网