【问题标题】:ajax post not trapping errors, always trapping successajax post 不捕获错误,总是捕获成功
【发布时间】:2017-08-30 21:11:23
【问题描述】:

我有一个 ajax post 方法可以将新记录添加到数据库中,该过程本身可以正常工作,并且我的服务器端代码会捕获错误(重复输入等)。当发生错误时,ajax 方法不会“看到”它,总是调用“成功”函数。这是我的ajax post方法

$.ajax({
                            url: '@Url.Action("Add", "Organisation")',
                            data: $form.serialize(),
                            async: true,
                            type: 'POST',
                            error: function (returnval) {
                                $form.parents('.bootbox').modal('hide');
                                bootbox.alert('There was an error saving this organisation : ' + returnval['responseText']);
                            },
                            success: function (returnval) {
                                // Hide the dialog
                                $form.parents('.bootbox').modal('hide');
                                bootbox.alert('New Organisation successfully added');
                            }
                        })

以及我控制器上的操作方法

[HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult Add(OrganisationEditCreateViewModel data)
    {
        InsertOrganisationRequest request = new InsertOrganisationRequest();
        IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {
            var model = mapper.Map<OrganisationEditCreateViewModel, OrganisationDto>(data);
            request.organisation = model;
            var response = this._organisationService.InsertOrganisation(request);
            if (response.isSuccess)
            {
                return Json(new { success = true, responseText = "New organisation added successfully" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { success = false, responseText = "Error adding new organisation : " + response.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        return Json(new { success = false, responseText = "Error adding new organisation : Invalid input" }, JsonRequestBehavior.AllowGet);
    }

因此,当我插入重复记录时,服务器端代码会捕获此记录,并将此代码分支返回给我的 ajax 调用

return Json(new { success = false, responseText = "添加新组织时出错:" + response.Message }, JsonRequestBehavior.AllowGet);

但是ajax调用总是调用这段代码

success: function (returnval) {
                                // Hide the dialog
                                $form.parents('.bootbox').modal('hide');
                                bootbox.alert('New Organisation successfully added');
                            }

错误部分永远不会被调用,我做错了什么?

【问题讨论】:

  • 您的方法中没有任何地方抛出异常,因此它不会命中error 函数。你需要检查returnval.success的值

标签: ajax asp.net-mvc error-handling controller actionmethod


【解决方案1】:

更新你的成功方法!

 success: function (returnval) 
        {
           if(returnval.success=true)
           {
             // Hide the dialog
             $form.parents('.bootbox').modal('hide');
            bootbox.alert('New Organisation successfully added');
           }
           if(returnval.success=false)
           {
             $form.parents('.bootbox').modal('hide');
             bootbox.alert('There was an error saving this organisation : ' + returnval['responseText']);
           }
        }

希望有所帮助!

【讨论】:

  • 不得不将 returnval.success=false 更改为 returnval.success==false 但这成功了,非常感谢,我已经尝试解决这个问题一段时间了。我不太明白错误的重点:功能部分,但如果它实际上不起作用:-(
  • 如果 ajax 调用出现错误,错误函数会被命中,但 ajax 调用工作正常并成功返回。现在您需要在成功方法中添加检查点。
【解决方案2】:

您没有做错任何事情。实际上您的代码也可以正常工作, 原因是 Success 函数总是执行,因为您的 AJAX 调用成功发生。

如果 AJAX 调用失败,则只会执行错误函数。

例如假设你给 Ajax 属性 contentType 错误

ex:- contentType:'xyzfds';

在某些情况下或者可能是因为任何其他 ajax 方法属性值错误。所以无需担心。

如果您想显示错误消息,请按照以下方法操作,它可能会对您有所帮助。谢谢

                     success: function (returnval) {
                           if(returnval!=null){
                           if(returnval.success){
                           // Hide the dialog
                            $form.parents('.bootbox').modal('hide');
                            bootbox.alert('New Organisation successfully added');
                         }
                         if(returnval.success==false){ 
                           $form.parents('.bootbox').modal('hide');
                            bootbox.alert('There was an error saving this organisation : ' + returnval['responseText']);
                    }                             
                   }
                  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2022-01-18
    • 2016-12-12
    • 2017-07-15
    • 2013-09-22
    相关资源
    最近更新 更多