Vue尝试解决跨域问题,但是出现报错

index.vue文件

mounted: function() {
            let _self = this;
            _self.$axios.post('/abo/abbott/list',{
                
                "stationName": "北土城"
            },{
                headers: {'Content-Type': 'application/x-www-form-urlencoded'} //加上这个
            })
                .then((res) => {
                    console.log('res == ', res);
                    // 成功后执行语句
                })
                .catch((err) =>{
                    // 网络中断或失败执行语句
                })

        }

代理的配置

proxyTable: {  
    '/abo/**': {  
        // target: 'http://129.28.127.53:8954/',  
  target: 'http://39.105.168.44:8021/',  
  changeOrigin: true,  
  pathRewrite: {  
            '^/abo': '/'  
  }  
    },  
  '/api/**': {  
        // target: 'http://129.28.127.53:8954/',  
  target: 'https://t.api.auto-pai.com',  
  changeOrigin: true,  
  pathRewrite: {  
            '^/api': '/'  
  }  
    },  
},

501.png

阅读 2.2k
1 个回答

如果后台接口地址是http://39.105.168.44:8021/abo/abbott/list的话,这样写就行了:

'/abo/**': {  
  target: 'http://39.105.168.44:8021'

如果后台接口址是http://39.105.168.44:8021/abbott/list,才需要这样:

 '/abo/**': {  
     target: 'http://39.105.168.44:8021/',  
     pathRewrite: {  
            '^/abo': '/'  
    }  

最好前缀固定为XX-api,比如abo-api。这样好标志是使用了代理。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题