https://blog.youkuaiyun.com/wh_xmy/article/details/87705840
main.js
import axios from 'axios'
Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api'
config index.js
proxyTable: {
//后加的
'/api': {
target:'http://123.57.103.198', // 你请求的第三方接口
changeOrigin:true, // 在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
pathRewrite:{ // 路径重写,
'^/api': '' // 替换target中的请求地址,也就是说以后你在请求http://api.douban.com/v2/XXXXX这个地址的时候直接写成/api即可。
}
},
//后加的
},
// proxyTable:{
// './': {
// target: 'http://www.xxx.cn/api', //你要访问的服务器域名
// changeOrigin: true, //允许跨域
// pathRewrite: {
// '^/': ''
// }
// }
// },
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}

本文介绍如何在Vue项目中集成Axios库,实现HTTP请求,并通过配置proxyTable解决跨域问题,使前端能顺利调用后端API。
682

被折叠的 条评论
为什么被折叠?



