vue 项目用 axios 进行请求的时候,总是报“Access to XMLHttpRequest at ‘http://localhost:8080/api/login’ from origin ‘http://localhost:8080……’”的错误
这属于跨域问题
解决方法:
找到项目的 config 下的 index.js
在 module.exports 中的 proxyTable 下添加下面的代码
proxyTable: {
"/api":{
target: 'http://localhost:8081/', //后端接口的根目录
changeOrigin: true, //是否跨域
pathRewrite: {
'^/api': '/'
}
}
}
然后在 main.js 下添加
axios.defaults.baseURL = '/api'
即用 /api 来代替原来的根目录
这样就能解决跨域问题啦,有帮助的点个赞哦!