如何处理 ionic 3 中的 http 错误响应?
how to handle http error response in ionic 3?
login(credentials) {
return new Promise((resolve, reject) => {
this.http.post(apiUrl+'loginjwt', credentials).subscribe(data => {
this.Storage.set("Access_Token", data['token']);
this.Storage.set("user", data["user"]).then(res => {
this.events.publish('user:loggedin',
{name: data['user']['first_name'] + ' ' + data['user']['last_name'],
id: data['user']['_id']});
}).catch((err) => {
console.log("failed to store");
console.log(err);
});
}, err => {
reject();
});
});
}
this.authServiceProvider.login(data).then((result) => {
let newdata = {"username": user['email'], "password": newpass1.value};
console.log(newdata);
this.authServiceProvider.update_pass(newdata).then((result) => {
if (result["message"]) alert("Password Update successfully!");
}, (err) => {
alert(err);
return false;
})
},(err) => {
alert("Password is not correct!");
});
我想实现的是在登录失败时提示用户密码错误。但我也收到了 HTTP 错误响应警报:
enter image description here
如何防止显示 http 警报?
您是否尝试在捕获错误时添加 httpErrorResponse,如下所示:
this.http.post(apiUrl+'loginjwt', credentials).subscribe(data => {
this.Storage.set("Access_Token", data['token']);
this.Storage.set("user", data["user"]).then(res => {
this.events.publish('user:loggedin',
{name: data['user']['first_name'] + ' ' + data['user']['last_name'],
id: data['user']['_id']});
}).catch((err) => {
console.log("failed to store");
console.log(err);
});
}, (err : HttpErrorResponse) => {
if (err.status == 400) {
alert("BAD REQUEST!");
} else if (err.status == 401) {
alert("password incorect!");
} else {
console.log(err);
reject();
}
});
login(credentials) {
return new Promise((resolve, reject) => {
this.http.post(apiUrl+'loginjwt', credentials).subscribe(data => {
this.Storage.set("Access_Token", data['token']);
this.Storage.set("user", data["user"]).then(res => {
this.events.publish('user:loggedin',
{name: data['user']['first_name'] + ' ' + data['user']['last_name'],
id: data['user']['_id']});
}).catch((err) => {
console.log("failed to store");
console.log(err);
});
}, err => {
reject();
});
});
}
this.authServiceProvider.login(data).then((result) => {
let newdata = {"username": user['email'], "password": newpass1.value};
console.log(newdata);
this.authServiceProvider.update_pass(newdata).then((result) => {
if (result["message"]) alert("Password Update successfully!");
}, (err) => {
alert(err);
return false;
})
},(err) => {
alert("Password is not correct!");
});
我想实现的是在登录失败时提示用户密码错误。但我也收到了 HTTP 错误响应警报: enter image description here
如何防止显示 http 警报?
您是否尝试在捕获错误时添加 httpErrorResponse,如下所示:
this.http.post(apiUrl+'loginjwt', credentials).subscribe(data => {
this.Storage.set("Access_Token", data['token']);
this.Storage.set("user", data["user"]).then(res => {
this.events.publish('user:loggedin',
{name: data['user']['first_name'] + ' ' + data['user']['last_name'],
id: data['user']['_id']});
}).catch((err) => {
console.log("failed to store");
console.log(err);
});
}, (err : HttpErrorResponse) => {
if (err.status == 400) {
alert("BAD REQUEST!");
} else if (err.status == 401) {
alert("password incorect!");
} else {
console.log(err);
reject();
}
});