【问题标题】:How to display Back-end response in Sweetalart2如何在 Sweetalert2 中显示后端响应
【发布时间】: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


    【解决方案1】:

    将标题改为error.message

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

    【讨论】:

    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2020-08-05
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2015-11-10
    相关资源
    最近更新 更多