vue跨域代理配置

實際:http://a.com/b.php
代理:http://localhost/b.php
配置config/index.jsphp

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

在main.js中:ios

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

在組件中使用:axios

this.axios.get('/n.php').then((data) => {
    // 處理數據
})

僅限於開發環境,生產環境仍然須要服務端設置容許跨域api

相關文章
相關標籤/搜索