vue-cli3 axios解决跨域问题
2024-09-03 23:05:34
这种错误就是跨域问题:
我百度了各种方法,最终下面这种方法解决了,直接上代码:
解决:
如果没安装axios:
npm install axios -save //安装axios
main.js
//引入axios
import axios from 'axios'
Vue.prototype.axios=axios axios.defaults.baseURL = '/api'
我用的是vue-cli3开发的项目,没有vue.config.js目录,这个是我新建的
vue.config.js中的代码:
module.exports = {
devServer: {
proxy: {
'/api': {
// 此处的写法,我访问的是http://localhost:8080/api/dataHome.json设置代理后,axios请求不需要把域名带上,只需要把路径前面加上 /api 即可。
target: 'http://localhost:8080/api/',
// 允许跨域
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
请求路径:
由于设置了pathRewrite
,所以请求时会把/api
替换成''
,最终的请求路径为 /dataHome.json
methods:{
http(){
let that=this;
this.axios.get("/dataHome.json")
.then((res)=>{
console.log(res);
}).catch((error)=>{
console.log(error)
})
}
}
重新运行
npm run serve
最后请求到了数据,嘿嘿
最新文章
- css3-无缝滚动
- Laravel Homestead安装笔记
- ArcGIS Add-in插件开发从0到1及实际案例分享
- 使用Qt installer framework制作安装包
- IOS: 模型面数控制
- 【转载】分析商品日均销量(DMS)对促销商品选择的意义
- uestc 1725 吴神数
- HDU 3665 Seaside (最短路,Floyd)
- solrcloud集群部署
- java基础IO删除文件夹文件
- HDU_1009_FatMouse' Trade
- Yii2基本概念之——属性(property)
- 数据库镜像转移Failover Partner
- angular ng-repeat 动态获取的dom片段 显示
- 【组合数】微信群 @upcexam6016
- java 中 热部署与卸载关系
- List排序Collections.sort 重写compare
- 17.async 函数
- Python全局变量和局部变量
- 使用supervisor过程中的一个问题
热门文章
- Java虚拟机-类文件结构
- 几个关于2-sat的题
- HDU5618 Jam's problem again CDQ分治
- hive查询中文乱码问题
- k8s的网络方案对比
- [译文] 为什么你在 C# 里总是应该使用 ";var"; 关键字
- monorepo仓库管理方式探秘
- Oracle 11g静默安装
- 洛谷$P$3066 逃跑的$BarnRunning\ Away\ From…$ $[USACO12DEC]$ 主席树
- 分布式唯一ID:雪花ID Snowflake .Net版