【发布时间】:2019-04-17 15:01:52
【问题描述】:
我正在尝试在 Sweetalert2 消息中显示后端错误响应。 我能够显示错误文本,但无法显示从后端获得的特定错误响应消息。
例如,如果我输入了错误的用户名,我会收到 API 响应“{“message”:“User Not Found”}”
如果我输入了错误的密码,我会收到 API 响应 "{"message":"Authentication Failed"}"
在以下代码中,我将 SweetAlert 标题更改为“标题:error.statusText”,它只显示“错误请求”
如何显示从后端收到的特定错误消息,而不是错误状态文本?
另外,我不知道为什么我的 console.log(error) 在以下代码块中不起作用。
signIn() {
this.authService.login(this.Username, this.Password).subscribe(
(data) => localStorage.setItem('Token', data),
(error) => Swal({
title: error.statusText,
type: 'warning',
confirmButtonText: 'Try Again'
}),
//console.log(error)
function (complete) {
Swal({
toast: true,
position: 'bottom',
type: 'success',
title: 'Login Successful!',
showConfirmButton: false,
timer: 3200
})
this.router.navigate(['/userDashboard']);
this.authService.setLoggedIn(true)
}.bind(this));
}
【问题讨论】:
标签: angular typescript sweetalert2