vue跨域代理配置

实际:http://a.com/b.php
代理:http://localhost/b.php
配置config/index.js

proxyTable: {
    '/api': {
        target:'http://a.com',
        changeOrigin: true,
         pathRewrite: {
          '^/api': '/'
        }
    } 
}

在main.js中:

import axios from 'axios'
Vue.prototype.axios = axios
axios.defaults.baseURL = '/api'

在组件中使用:

this.axios.get('/n.php').then((data) => {
    // 处理数据
})

仅限于开发环境,生产环境仍然需要服务端设置允许跨域

posted @ 2017-09-22 16:02  wmui  阅读(6036)  评论(0编辑  收藏  举报