vue 进行 get 请求时,跨域配置不生效(后端运行在本机8089端口,vue运行在8080端口)

vue.config.js配置代理:

 devServer: {
    proxy: {
      '/api': {
        target: 'https://i.maoyan.com/',// 目标路径
        changeOrigin: true,// 允许跨域
        pathRewrite: {// 重写跨域
          '^/api': ''
        }
      }
    }
  }

组件中调用:

axios({
        url: "/api/api/mmdb/movie/v3/list/hot.json",
        method: "get",
        params: {},
      })
        .then((response) => {
          console.log(response);
        })
        .catch((error) => {
          console.log(error);
        });

但是打印台报错:

localhost:8080/api/api/mmdb/movie/v...
配置代理中proxy的’/api’在请求路径中直接为字符串,并未生效,请问这个需要怎么解决?

讨论数量: 4
cnguu

先确保 https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json 能直接打开

然后 /api 改下其他试试,例如:/dev-api

1年前 评论
Skye77 (楼主) 1年前
cnguu

再试试下面几种:

  1. http
  2. 不写 pathRewrite 的用法
  3. 加个斜杠
    '^/api': '/'
  4. 配置下 axiosbaseURL
1年前 评论

axios 是不是配置了 baseURL, 如果配置了, 那就把 pathRewrite 中的内容给删了试试看, 而且, 8089 端口有配置吗?

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!