は、私は時々、このエラーを取得API呼び出しがあります。私のコードでHttpErrorResponseのインスタンスが、HttpClientを使用してAngularでサーバーレスポンスのサブスクリプションのエラー関数をトリガーしないのはなぜですか?
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …} error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …}headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}message: "Http failure response for (unknown url): 0 Unknown Error"name: "HttpErrorResponse"ok: falsestatus: 0statusText: "Unknown Error"url: null }
を、サーバーの呼び出しがそうのように扱われている...(免責事項、いくつかのコードは、簡潔のために簡単なバージョンに縮小されています)
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
getData() {
const apiRoot = 'http://whatever.../getSomething...';
const params = new HttpParams().set('foo', 'bar');
const body = { params };
return this.http.get(apiRoot, body)
.map(res => res)
.catch(err => Observable.of(err));
}
サーバの応答がそのように好きに加入している...
this.getData().subscribe(
response => {
this.setApiResultsData(response);
console.log(response); // <-- where the "error" is printed
},
error => {
this.setApiResultsError(error.message);
console.log(response); // <-- where I would think the error would go is never triggered... ???
}
);
私の質問は、なぜそれをSにerror
に渡されませんされてubscription?私は何か間違っているのですか?
.catch(err => Observable.of(err));
したがって、呼び出し方法がない:あなたは、本質的にそれをキャッチし、新しい観測可能を返すことによって、エラーから回復している私は、正しくエラーを処理したいと思いますので、私はgetData
インサイド提案
ああ、完璧な意味があります。ありがとう。それはCORSの問題でもありますが、なぜ0のステータスコードがエラーではないのかが明確ではありませんでした。 –