问题描述:
Vue发送请求的时候存在跨域,不能请求到正确数据,控制台报错如下:
Access to XMLHttpRequest at 'http://localhost:8000/equip_fault_report/all' from origin 'http://localhost:8080' has been
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
解决方案: webpack
的代理proxy
配置
配置代理如下:
vue-cli3.0
以前版本配置/config/index.js
文件:
module.exports = {
dev: {
proxyTable: {
'/api': {
target: 'http://10.10.17.64:8082/',//设置你调用的接口域名和端口号 别忘了加http
changeOrigin: true,//设置true 代表跨域访问
secure: false,// 如果是https接口,需要配置这个参数
pathRewrite: {
'/api': ''
}
}
}
}
}
vue-cli3.0
以后的版本配置vue.config.js
文件:
// An highlighted block
var foo = 'bar';
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://10.10.17.64:8082/',//设置你调用的接口域名和端口号 别忘了加http
changeOrigin: true,//设置true 代表跨域访问
secure: false,// 如果是https接口,需要配置这个参数
pathRewrite: {
'/api': ''
}
}
}
}
}
解读为加连接头,开启跨域,加/api
作为识别。
意为请求/api
下的链接,直接回转到target