I am currently utilizing HTTP requests in Angular 2. My objective is to trigger the next process once I receive a response from the HTTP request.
For example: In a form, the select options are populated through an HTTP GET request. I aim for the form page to remain loading until I receive a response with the select options.
Function to retrieve select option value:
getSelectOptionValue(): any {
let area_list_url = '/select_option_list/';
this.urlGet(area_list_url).subscribe(
(response) => {
let data = response.text() ? response.json() : [{}];
if (data) {
Constant.areaList = data;
}
}
);
return JSON.stringify(Constant.areaList);
}
Function for GET request:
urlGet(url: string) {
return this._http.get(Constant.hostUrl + url, {headers: GlobalUtils.head})
.map((res)=> {
if (res.status === 200) {
console.log(res);
return res;
} else if (res.status = 201) {
return res;
}
}).catch((error)=> {
console.log(error);
if (error.status === 400) {
return Observable.throw(new Error(error.status));
} else if (error.status === 401) {
return Observable.throw(new Error(error.status));
} else if (error.status === 403) {
return Observable.throw(new Error(error.status));
} else if (error.status === 404) {
return Observable.throw(new Error(error.status));
} else if (error.status === 420) {
return Observable.throw(new Error(error.status));
} else {
return Observable.throw(new Error(error.status));
}
});
}