订阅者内部的订阅者在离子项目中无法正常工作
Subscriber inside subscriber not working properly in ionic project
我正在为 android 开发离子应用程序。我在全球范围内发出 http 请求,并根据响应处理该请求以进行分页。例如,如果响应不包含 header 中的授权密钥,我将在全局声明的服务 class.
中发送错误消息
这里页面订阅方法-
this.loginService.login(params)
.subscribe(resp => {
//processing response
},
error => {
this.alertService.presentToast(AppConstants.NETWORKERRMSG);
this.loading.dismiss();
}
中间(login-Service)服务调用http服务-
login(params){
return this.netHelper.post('/method',
{ params } ) }
和 netHelper 中的全局 http 服务方法 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;
}
尝试像这样更改 netHelper class 中的 post 方法,它将按预期工作-
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;
});
}
我正在为 android 开发离子应用程序。我在全球范围内发出 http 请求,并根据响应处理该请求以进行分页。例如,如果响应不包含 header 中的授权密钥,我将在全局声明的服务 class.
中发送错误消息这里页面订阅方法-
this.loginService.login(params)
.subscribe(resp => {
//processing response
},
error => {
this.alertService.presentToast(AppConstants.NETWORKERRMSG);
this.loading.dismiss();
}
中间(login-Service)服务调用http服务-
login(params){
return this.netHelper.post('/method',
{ params } ) }
和 netHelper 中的全局 http 服务方法 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;
}
尝试像这样更改 netHelper class 中的 post 方法,它将按预期工作-
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;
});
}