ASP.NET中的AJAX错误c#

Tul*_*ips 2 c# ajax asp.net-mvc

我是Ajax和ASP.NET MVC的新手.我有一个函数,返回到AJAX,我需要处理错误情况.当一切正常时,代码就可以了.我的问题是如何处理错误部分.这是我有的:

为了取得成功,我有:

var data = new { success = false };

return Json(data, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)

我需要知道在出现异常或错误时要返回什么?

这是我的查询:

function DoMailPDF() {
             $("#submitMail").attr("disabled", true);
             var personid = $("#personid").val();
             var unitid = $("#unitid").val();
             var url = "@(Url.Action("SendEmail", "Report"))";
            $.ajax({
                url: url,
                data: { person: personid , unit:unitid},
                success: function () {                    
               // $('input[name=MailSent]').attr('checked', true);
                $("#submitMail").removeAttr("disabled");
                    alert("Email sent!");
                },
                error: function () {                    
                 alert("Email not sent!");
                }
            });
        }
Run Code Online (Sandbox Code Playgroud)

它永远不会出现错误功能.如何使它出错?任何提示和建议都是最受欢迎的.

iap*_*dev 5

您可以通过编写以下内容来访问json响应对象:

       $.ajax({
            url: url,
            data: { person: personid , unit:unitid},
            dataType: 'json',
            success: function (response) {                    
                if (response.success == false) {
                    // Error handling
                } else {
                    // Success handling
                }
            },
            error: function () {                    
             alert("Email not sent!");
            }
        });
Run Code Online (Sandbox Code Playgroud)