我正在使用 fetch api 发送请求,并根据结果正确或包含错误采取行动。
我的服务代码:
LoginUser(user: User) {
return fetch("http://localhost:8080/login", {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(user)
});
}
Run Code Online (Sandbox Code Playgroud)
我then-catch调用上面代码的代码是:
async loginUser() {
let response = await this._service.LoginUser(this.user)
.then(response => {return response.json()})
.then(response => {this._router.navigate(['/mainpage'])})
.catch(error => {alert(error)});
}
Run Code Online (Sandbox Code Playgroud)
响应是否带有代码 500 内部服务器错误,仍然重定向到/mainpage并且无法识别该错误。我该如何解决这个问题?