vue 本地调试跨域---带cookies(axios)

cookise跨域第二期之便捷优雅的本地调试(axios)

1、打开config/index.js,在proxyTable中添写如下代码:

proxyTable: {
      '/agent': {  //使用"/agent"来代替源地址
        target: 'https://datacloudtest.mncats365.com', //想要访问的地址
        secure: true,//如果是https请设置为true
        changeOrigin: true, //改变源
        pathRewrite: {
          '^/agent': 'http://localhost:8081' //本地路径
        }
      }
    },

2、使用axios请求数据时直接使用“/agent”:

如果没有自行封装axios,就直接在main.js中引入

import axios from 'axios'

Vue.prototype.$axios=axios

使用

this.$axios.get('/agent/web/followMac/getFollowMacList').then(res => {
          console.log(res)
        }).catch(error => {
          console.log(error)
        })

如果自行封装axios——直接写

http.get('/web/followMac/getFollowMacList', {}).then().catch()

不过需要在配置axios的config的baseURL,配置在config的baseURL会在url参数不为http开头时添加在url前面

baseURL: '/agent',//本地测试
baseURL:'正式服务器路径'

这样就可以愉快的本地调试了,是利用node服务进行代理

猜你喜欢

转载自www.cnblogs.com/hsBK/p/12726573.html
今日推荐