【问题标题】:How to properly catch errors in SweetAlert如何正确捕获 SweetAlert 中的错误
【发布时间】:2017-08-28 10:42:55
【问题描述】:

我有以下swal 代码,如果 HTTP 调用的返回状态为“200”,它可以正常工作。但是,如果我收到“403”未经授权的错误,swal 加载程序会继续旋转,并且消息永远不会消失,如下所示。我不确定我是否没有正确处理错误。

你能帮忙吗?

let that = this;
swal({
  title: 'Are you sure?',
  text: 'You won\'t be able to revert this!',
  type: 'warning',
  showCancelButton: true,
  confirmButtonText: 'Yes, delete it!',
  confirmButtonColor: '#d33',
  cancelButtonText: 'No, keep it',
  showLoaderOnConfirm: true,
  preConfirm: function (email) {
    return new Promise(function (resolve, reject) {

      that.http.get(url, {"headers": headers})
        .map(res => res)
        .subscribe(data => {
          console.log(data);
          if (data.status === 200) {
            resolve();
          } else {
            reject(data.status);
          }
      })          
    })
  },
  allowOutsideClick: false
}).then(function() {
  swal(
    'Deleted!',
    'The user has been deleted.',
    'success'
  )
}, function(dismiss) {
  if (dismiss === 'cancel') {
    swal(
      'Cancelled',
      'Nothing has been deleted!',
      'error'
    )
  }
}).catch((error) => {
  console.log('error_does_not_reach_here', error);
})

【问题讨论】:

    标签: sweetalert sweetalert2


    【解决方案1】:

    这是我的代码示例:

     swal({
                    title: "Confirm",
                    text: "¿Are you sure?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonText: 'Aceptar',
                    cancelButtonText: 'Cancelar'
                }).then(function () {
                 //If Press ok, send the data
                    var arr = {
                        cuil: empDP.fd.Cuil,
                        cct: empDP.fd.Cct,
                        activity: empDP.fd.Activity,                   
    
                    };
                    Vue.http.post(empDP.globalurl + "/persondata/save", arr).then(function (response) {
                        swal('Data saved correctly', '', 'success');
                        empDP.clean();
                    }, function (response) {
                   //If Response return error
                        swal('Show a Text Error', '', 'error');
                    });
    

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多