When sending a POST request to a backend and attempting to read the status code, I encountered the following result: status-code:0
Below are my functions:
Service:
signIn(uname:string, pass:string): Observable<any> {
let headers = new Headers({
'Content-Type': 'application/json',
'Uname':uname,
'Password':pass
});
let opt = new RequestOptions({ headers: headers, withCredentials: true });
let data = {};
return this.http.post( url, data, opt )
.map(res => {
return res.json();
})
.catch( this.handleError );
}
private handleError(error:Response | any){
let details = error.json();
console.log('STATUS CODE: ' + error.status);
return Observable.throw(details || error);
}
Component:
login(uname: string, pass: string) {
this.user.signIn(uname, pass).subscribe(
res => {},
err => {
console.log('ERR: ' + JSON.stringify(err));
// returns {"isTrusted":true}
}
);
}
I aim to extract the status code like 404, 401, or 403. In my case, I can observe the status 404 in Chrome console, hence I would like to exhibit an errorMsg corresponding to the status code error.
Upon switching to the Network
tab in the console, under the Response
section, I find:
{"code":404,"reason":"Not Found","message":"Resource 'user/auth' not found"}
Further outputting error
within the handleError()
function fetches:
handleErrorObservable: {"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null}
Is there an approach to resolve this issue based on the provided code, or where could the mistake be occurring?