我正在尝试使用以下请求对外部API进行HTTP requeset:
let headers = new Headers({
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': "*"
});
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify({
test: test
comments: "test"
});
this.http.post("EXTERNAL_API_URL", body, options)
.map(res => res.json())
.subscribe((data) => {
console.log(data);
});
当我从Chrome Postman插件进行API调用时,API工作没有任何问题。
当我尝试从我的角度2应用程序进行此调用时,调用无效并返回以下错误:
XMLHttpRequest cannot load MY_API_URL. 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:4200' is therefore not allowed access.
我知道我可以使用no web-security chrome访问它:
Chrome.exe --disable-web-security
但我希望没有它就能让它发挥作用......
我还尝试在我的Header set Access-Control-Allow-Origin "*"
中添加http.conf
,如here所述。
有什么想法吗?