I am developing ionic app for android. I am making http request globally and based response I am processing that request to page. Example, If response not contains auth key in header I will send error message in globally declared service class.
here page subscriber method-
this.loginService.login(params)
.subscribe(resp => {
//processing response
},
error => {
this.alertService.presentToast(AppConstants.NETWORKERRMSG);
this.loading.dismiss();
}
middle (login-Service)service to call http service-
login(params){
return this.netHelper.post('/method',
{ params } ) }
and global http service method in netHelper class-
post(url: string, params: any) {
var response: any ;
this.http.post<any>(url, params,
{headers})
}).subscribe(data => {
console.log(data.status);
console.log(data);
response = data;
},
error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
return response;
}
try to change the post method in the netHelper class like this , it will work as expected-
let respbody: any = '';
post(url: string, params: any) {
return this.http.post(url, params,
{
headers: headers,
observe: 'response'
}).
map(data => {
let respbody: any = '';
let resp: any = data;
if (resp.status == '200') {
respbody = resp.body;
}
else if (resp.status === '401') {
this.alertService.presentToast(SECURITYMSG);
}
else {
this.alertService.presentToast(NETWORKERRMSG);
}
return respbody;
});
}