My component template url points to server that generates the response html and it may return different http error status codes (401, 403 etc') when something goes wrong. If an error occurs while loading the route I would like to get the error status. Using router.events.subscribe and checking for NavigationError doesn't provide the http error status only some text - "failed to load url/to/my/template". Any idea how to get the response http status code?
Asked
Active
Viewed 563 times
1 Answers
0
This functions makes HTTP request with URL as a parameter.
get_http_response(url) {
this._http.get(url) /*this make http request to server*/
.map((response: Response) => {
response.json();
this.responseStatus = response.status; /* local variable to restore status code. */
}) /*this get response back and map it*/
// ...errors if any
.catch((error: any) => Observable.throw(error.json().error || 'Server error')
)
.subscribe(
resdata => {
this.getdata = resdata; /*local variable to store data. */
console.log("fetched data =>" +this.getdata);
console.log("status code =>" + this.responseStatus);
}
);
}
I think it will solve your problem if you get any error comment on below post.
Thank You.

Deepak Chawla
- 297
- 2
- 6