关于javascript:webpack-跨域-proxy-url地址后面被匹配的坑-模糊-绝对地址

52次阅读

共计 1274 个字符,预计需要花费 4 分钟才能阅读完成。

谬误的示范

proxy: {
// 配置跨域
'/api': {
target: 'https://xx.xx.com:18083',
changOrigin: true,
pathRewrite: {'^/api': ''}
},
'/user': {
target: 'https://xx.xx.com/as',
changOrigin: true,
pathRewrite: {'^/user': ''}
},
'/qq': {
target: 'https://api.weixin.qq.com/',
changeOrigin: true,
pathRewrite: {'^/qq': ''}
}
}

获取接口时

// 微信申请
get_token(url) {
return axios({
method: 'get',
baseURL: process.env.NODE_ENV === 'development' ? '/qq' : 'https://api.weixin.qq.com/',
url,
timeout: 10000,
headers: {// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',},
}).then((response) => {return checkStatus(response)
}
).then((res) => {return checkCode(res)
}
)
},
// 用公众号信息和 code 获取 accessToken 和 openid
accessToken: async function(url) {const res = await http.get_token(url)
if (res.status === 200) {const { access_token, openid} = res.data
const userUrl = `sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`
this.getUserInfo(userUrl)
}
},

外面的 userinfo 和配置的跨域里的/user

const userUrl = `sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`

解决办法:

  • /user 改个名字(只有蕴含就会匹配,不蕴含就行)。如:/userurl
proxy: {
// 配置跨域
'/api': {
target: 'https://xx.xx.com:18083',
changOrigin: true,
pathRewrite: {'^/api': ''}
},
'/userurl': {
target: 'https://xx.xx.com/as',
changOrigin: true,
pathRewrite: {'^/userurl': ''}
},
'/qq': {
target: 'https://api.weixin.qq.com/',
changeOrigin: true,
pathRewrite: {'^/qq': ''}
}
}

正文完
 0