vue.js 跨域请求代理

 

一:跨域请求代理

1:打开config/index.js,增加proxyTable内容

​
module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'https://www.zhangzhijian.com',  //源接口地址,network请求地址中api前面的部分
        changeOrigin: true, //改变源
 pathRewrite: {
          '^/api': ''//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
        }
      }
    },

​

2:在需要调接口的组件中这样使用:

axios.post('/api/yt_api/login/doLogin',postData)
    .then(function (response) {
        console.log(1)
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    })

 注意:原接口:http://www.zhangzhijian.com/yt_api/login/doLogin

    页面调用:http://localhost:8081/api/yt_api/login/doLogin ——这里多了一个/api/不是多余的,不要删

猜你喜欢

转载自blog.csdn.net/namechenfl/article/details/81775037