Vue配置跨源携带cookies

2020-10-20  本文已影响0人  王二麻子88

Vue配置跨源携带cookies

在开发环境中, 有时需要同源携带cookie

在 axios 中配置

import axios from "axios";
service = axios.create({
    baseURL: "/", // api的base_url
    timeout: 50000 // 请求超时时间
});
service.defaults.withCredentials = true;

export default service;

在 vue.config.js(没有在项目根目录中新建一个)中配置

module.exports = {
    devServer: {
    proxy: {
        '/wbr': {
        target: 'http://192.168.5.2/',  // 指向的后台接口地址
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '/wbr': ''
        }
      },
    }
  },
}

应用:

window.$common.baseUrl = "/ljy";

import service from "../service";

service({
    url: `${window.$common.baseUrl}/getWebSizeImageList`,
    method: "POST"
}).then((res)=>{
    console.log(res.data);
})
上一篇 下一篇

猜你喜欢

热点阅读