我希望通过使用代理将我从localhost发送的请求传递给远程服务器,我无法控制。因此http://localhost:8000/api/Lists/GetCarList
将被解析为https://other-site.ru/api/Lists/GetCarList
但是根据我的解决方案,请求仍然以http://localhost:8000/api/Lists/GetCarList
我正在使用ng serve --proxy-config proxy.conf.json --port 8000
我的proxy.conf.json文件
{
"/coreapi/*": {
"target": "https://other-site.ru",
"secure": false,
"pathRewrite": {
"^/": ""
},
"changeOrigin": true
}
}
我的要求
var headers = new Headers();
headers.append('Authorization', 'Bearer ' +
this.oauthService.getAccessToken());
headers.append('Content-Type', 'application/json;charset=UTF-8');
let options = new RequestOptions({headers: headers});
return this.http.post('/coreapi/UserService/GetUserServersList', {}, options)
.toPromise().then((response: Response)=>{
var resp = response;
},
err =>{
console.log("err");
})
那么,问题是如何解决这个问题,以便在/coreapi/**
处获取https://other-site.ru/coreapi/**
的相对请求?
答案 0 :(得分:0)
试试这个:
"pathRewrite": {"^/coreapi" : "https://other-site.ru/coreapi"}
答案 1 :(得分:0)
解决了这个问题,我必须在生成的webpack.config.js文件"devServer": { "historyApiFallback": true, "watchOptions": { "aggregateTimeout": 300, "poll": 1000 }, "headers": { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS", "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization" } }
中运行ng eject并启用cors。