如何在Sweetalart2中显示后端响应

时间:2019-04-17 15:01:53

标签: angular typescript sweetalert2

我正在尝试在Sweetalert2消息中显示后端错误响应。 我能够显示“错误”文本,但无法显示从后端获取的特定错误响应消息。

例如,如果我输入了错误的用户名,则会收到API响应“ {“ message”:“找不到用户”}“

如果我输入了错误的密码,我将得到API响应“ {“ message”:“ Authentication Failed”}“

在以下代码中,我将SweetAlert标题更改为“ title: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));
}

1 个答案:

答案 0 :(得分:0)

将标题更改为error.message

(error) => Swal({
    title: error.message,
    type: 'warning',
    confirmButtonText: 'Try Again'
}),