这种错误就是跨域问题:

我百度了各种方法,最终下面这种方法解决了,直接上代码: 

解决:

如果没安装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

最后请求到了数据,嘿嘿

最新文章

  1. css3-无缝滚动
  2. Laravel Homestead安装笔记
  3. ArcGIS Add-in插件开发从0到1及实际案例分享
  4. 使用Qt installer framework制作安装包
  5. IOS: 模型面数控制
  6. 【转载】分析商品日均销量(DMS)对促销商品选择的意义
  7. uestc 1725 吴神数
  8. HDU 3665 Seaside (最短路,Floyd)
  9. solrcloud集群部署
  10. java基础IO删除文件夹文件
  11. HDU_1009_FatMouse' Trade
  12. Yii2基本概念之——属性(property)
  13. 数据库镜像转移Failover Partner
  14. angular ng-repeat 动态获取的dom片段 显示
  15. 【组合数】微信群 @upcexam6016
  16. java 中 热部署与卸载关系
  17. List排序Collections.sort 重写compare
  18. 17.async 函数
  19. Python全局变量和局部变量
  20. 使用supervisor过程中的一个问题

热门文章

  1. Java虚拟机-类文件结构
  2. 几个关于2-sat的题
  3. HDU5618 Jam's problem again CDQ分治
  4. hive查询中文乱码问题
  5. k8s的网络方案对比
  6. [译文] 为什么你在 C# 里总是应该使用 "var" 关键字
  7. monorepo仓库管理方式探秘
  8. Oracle 11g静默安装
  9. 洛谷$P$3066 逃跑的$BarnRunning\ Away\ From…$ $[USACO12DEC]$ 主席树
  10. 分布式唯一ID:雪花ID Snowflake .Net版