我在前端的请求是这样的:
//获取强档推荐数据
$.ajax({
url: '/ly/Recommend/get/',
data: {
adpos: 907,
num: 24,
pool_id: 302
},
success(data){
let obj = JSON.parse(data);
console.log(obj);
},
error(error){
console.log(error);
}
});
我在服务端用 http-proxy-middleware 做了代理:
let o = {
target: 'https://ac.qq.com/',
changeOrigin: true,
pathRewrite: {
'^/ly': ''
}
};
app.use('/ly', proxy(o));
用谷歌浏览器测试,pc 端请求成功,而移动端模式报错:
Failed to load https://m.ac.qq.com/Recommend/get/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8088' is therefore not allowed access.
可以看到,请求地址代理到了 https://m.ac.qq.com/,但是我明明写的是 https://ac.qq.com/ ,请问这个问题该怎么解决?
移动端请求 ac.qq.com 时服务端自动重定向到了 m.ac.qq.com ,需要在 CORS 设置中加上 https://m.ac.qq.com/
明白了,我需要在前端做 pc 端、移动端的判断,对应到服务端不同的代理。谢谢~