1. 程式人生 > >vue解決POST跨域請求

vue解決POST跨域請求

一.設定config/index.js || proxyTable新增

proxyTable: {
     '/api': {
            target: 'http://192.168.48.239:8080/ydzl',
            changeOrigin: true,
            pathRewrite: {
                '^/api': ''
            }
      }

二.mian.js 新增vue全域性屬性

Vue.prototype.HOST = '/api'

三.如果是post的話

1.修改資料格式

transformRequest: [function (data) {
                    // Do whatever you want to transform the data
                        let ret = ''
                        for (let it in data) {
                          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        return ret
                      }],

2.修改請求頭

headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }