Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

vue-cli3设置proxy代理访问外部接口失效

我的

devServer: {
    proxy:  {
      '/proxy': {
        target: 'https://dic.pixiv.net/api/',  // 外部接口地址
        changeOrigin: true,   // 是否允许跨域
        pathRewrite: {  // 重写路径 目的是把/proxy 开始的接口重写 
          '^/proxy': ''
        }
      },
    },

接口访问

export function getTagRead(tagName) { 
 return http.get(`/proxy/tag_count/${tagName}?json=1`)
}

我希望该接口访问的地址:

https://dic.pixiv.net/api/tag_count/名字?json=1

可实际上

image.png

127.0.0.1:3000 是我配置的开发环境下的前缀

image.png

这相当于上面我配置里写的proxy重写路径根本没有生效,请问要如何解决?

是因为axios设置的默认前缀覆盖了proxy配置吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

"/proxy": {

target: "https://dic.pixiv.net/api",
ws: true,
changeOrigin: true,
pathRewrite: {
    "^/proxy": "/"
}

},

试试这样 请求的baseurl去掉试试 修改配置文件也都需要重启


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...