我正在尝试在Angular2上发送POST
请求,但是我收到以下错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我已经添加了Access-Control-Allow-Origin: *
,如果我尝试使用Postman发送请求,请求就可以了。但是,它在我的Angular2应用程序中不起作用:
为MyService
sendRequest(id: string, state: boolean): Observable<any> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let url = 'http://MY-URL-HERE';
return this.http.post(url, {id, state}, options)
.map((res: Response) => {
return res.json();
})
.catch((err: any) => {
return Observable.throw(err);
});
}
MyComponent的
change(id, event) {
this.service.sendRequest(id, event)
.subscribe(res => console.log(res));
}