尽管出现 XMLHttRequest 错误,Angular2 post 请求

Angular2 post request despite a XMLHttRequest error

我向远程发送请求 API。 API 需要一点时间才能站在一边。

经过这段短暂的等待时间后,我可以在网络选项卡中看到一个 HTTP 200。在响应中,我获得了正确的预期信息。 API 方面的一切工作正常。

BIT 在控制台上,我可以看到我遇到了 XMLHttpRequest 错误。

为什么,特别是当我遇到 XMLHttpRequest 错误时,POST 以 200 完成? Angular2不应该是"blocked"吗?

意外结果是:我的文件已由 API 正确上传和处理,但在 Angular2 中,它触发了我调用的错误部分.

例如,如果我使用 https://resttesttest.com/,它似乎会遇到同样的错误,但它不会最终确定 POST:

Oh no! Javascript returned an HTTP 0 error. One common reason this might happen is that you requested a cross-domain resource from a server that did not include the appropriate CORS headers in the response.

Angular 2 本次调用的代码

this.http
    .post(this.documentUploadAPIUrl, formData, options)
    .subscribe(
      res => {
        this.responseData = res.json();
        console.log(this.responseData);
        console.log('Uploaded a blob or file!');
      },
      error => {
        console.log('Upload failed! Error:', error);
      }
    );

尝试将 xmlHttpRequest 的 withCredential 属性设置为 true,这将发送由浏览器管理的凭据,在 angular2 中你可以这样做

    import { RequestOptions } from '@angular/http';


    this.http
      .post(this.documentUploadAPIUrl, formData, this.post_options)
      .subscribe(
        res => {
         this.responseData = res.json();
         console.log(this.responseData);
         console.log('Uploaded a blob or file!');
        },
        error => {
         console.log('Upload failed! Error:', error);
        }
       );



post_options() {
   return new RequestOptions({ method: 'post', withCredentials : true  });
}