iview-cli 项目、iView admin 跨域问题解决方案
在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
此处以本机启动服务器为例
请根据具体情况自行更改