POST请求适用于Postman但不适用于Angular2

时间:2016-11-11 17:33:28

标签: javascript post angular http-post

我正在尝试在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));
}

0 个答案:

没有答案