ajax帖子没有捕获错误,总是陷阱成功

时间:2017-04-05 10:07:09

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

我有一个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调用

返回Json(new {success = false,responseText =&#34;添加新组织时出错:&#34; + response.Message},JsonRequestBehavior.AllowGet);

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

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

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

2 个答案:

答案 0 :(得分:0)

更新您的成功方法!

 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']);
           }
        }
希望有所帮助!

答案 1 :(得分:0)

你没有做错任何事。实际上你的代码工作正常        原因是成功函数总是在执行,因为您的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']);
                    }                             
                   }
                  }
相关问题