您的位置:首页 > 其它

iview-cli 项目、iView admin 跨域问题解决方案

2017-10-24 11:25 381 查看

在build 目录的 webpack.dev.config.js 目录中

module.exports = merge(webpackBaseConfig, {
devtool: '#source-map',
output: {
publicPath: '/dist/',
filename: '[name].js',
chunkFilename: '[name].chunk.js'
},
plugins: [
new ExtractTextPlugin({
filename: '[name].css',
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
filename: 'vendors.js'
}),
new HtmlWebpackPlugin({
filename: '../index.html',
template: './src/template/index.ejs',
inject: false
})
],
//设置跨域代理
devServer: {
historyApiFallback: true,
hot: true,
inline: true,
stats: { colors: true },
proxy: {
//匹配代理的url
'/api': {
// 目标服务器地址
target: 'http://127.0.0.1:8081',
//路径重写
pathRewrite: {'^/api' : '/api'},
changeOrigin: true
}
}
}

请求时

//引入axios
import axios from 'axios';

//请求方法,根据实际情况使用
axios.get('/api/user').then((res) => {
//res 为成功回调的响应
console.log(res);
});

配置后则可以将请求转发到

http://127.0.0.1:8081

此处以本机启动服务器为例
请根据具体情况自行更改
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: