在不同域之間訪(fǎng)問(wèn)是比較常見(jiàn),在本地調(diào)試訪(fǎng)問(wèn)遠(yuǎn)程服務(wù)器跃惫。叮叹。。爆存。這就會(huì)牽扯跨域衬横。
本文主要講述開(kāi)發(fā)環(huán)境
VUE解決通過(guò)proxyTable:
-
在 config/index.js 配置文件中
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
//proxyTable: {},
proxyTable:{
'/apis': {
// 測(cè)試環(huán)境
target: 'http://api.veblen.com', // 接口域名
changeOrigin: true, //是否跨域
pathRewrite: {
'^/apis': 'apis' //需要rewrite重寫(xiě)的,
}
},
// 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
}
-
核心配置
proxyTable: {
'/apis': {
// 測(cè)試環(huán)境
target: 'http://api.veblen.com', // 接口域名
changeOrigin: true, //是否跨域
pathRewrite: {
'^/apis': 'apis' //需要rewrite重寫(xiě)的,
}
}
}
-
Vue請(qǐng)求
this.$http.get( '/apis/getData')
.then(function(res){
// 成功回調(diào)
},function(){
alert("error")
})
請(qǐng)求路徑就會(huì)被映射http://api.veblem.com/apis/getData