vue脚手架的跨域配置
module.exports = {
devServer: {
open: true,
proxy: {
'/getDiscList': {
target: 'http://localhost:8000/tp5/publica/index/Cook/show',// 要跨域的域名
changeOrigin: true, // 是否开启跨域
pathRewrite: {
'^/api': '/api/' // 在请求的时候 凡是/api开头的地址都可以跨域
}
},
}
}
};
fetch('http://localhost:7000/getDiscList').then(x=>x.json())
.then(res=>{
console.log(res)
})
或者
如果不说明url地址 会默认取服务器的地址localhost什么端口
axios({
url:'/index/index',
baseURL: '/tp5/',
method: 'get'
}).then(x=>{
console.log(x)
})
vue.config.js文件如下
module.exports = {
devServer: {
open: true,
proxy: {
'/tp5': {
target: 'http://localhost:3000',// 要跨域的域名
changeOrigin: true, // 是否开启跨域
pathRewrite: {
'^/api': '/api/' // 在请求的时候 凡是/api开头的地址都可以跨域
}
},
}
}
};
如果不配置vue.config.js 就使用nginx 先把脚手架打包
然后把dist文件放入
就可以跑起来了
版权声明
本文仅代表作者观点,不代表博信信息网立场。