vue脚手架解决跨域问题-------配置反向代理

1.打开config/index.js

2.在dev配置对象中找到proxyTable:{}

3.添加如下配置

 // 配置反向代理,解决跨域请求
    proxyTable: {
      '/api': {
        target: 'http://www.ajax.cn',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/api'
        }
      }
    },

4.重启开发服务器npm run dev

发送请求示例

// 导入axios
import axios from 'axios'
axios.defaults.baseURL='http://localhost:8080'
axios.get('/api/json.php').then(function(res){
          console.log(res);
});