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’在请求路径中直接为字符串,并未生效,请问这个需要怎么解决?
先确保
https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json
能直接打开然后
/api
改下其他试试,例如:/dev-api
再试试下面几种:
http
pathRewrite
的用法axios
的baseURL
axios
是不是配置了baseURL
, 如果配置了, 那就把pathRewrite
中的内容给删了试试看, 而且, 8089 端口有配置吗?