Can I return custom error from JsonResult to jQuery ajax error method?

前端 未结 5 2034
温柔的废话
温柔的废话 2020-12-07 16:06

How can I pass custom error information from an ASP.NET MVC3 JsonResult method to the error (or success or complete, if n

5条回答
  •  执笔经年
    2020-12-07 16:36

    My MVC project wasn't returning any error message (custom or otherwise). I found that this worked well for me:

    $.ajax({
            url: '/SomePath/Create',
            data: JSON.stringify(salesmain),
            type: 'POST',
            contentType: 'application/json;',
            dataType: 'json',
            success: function (result) {
    
                alert("start JSON");
                if (result.Success == "1") {
                    window.location.href = "/SomePath/index";
                }
                else {
                    alert(result.ex);
                }
    
                alert("end JSON");
            },
            error: function (xhr) {
    
                alert(xhr.responseText);
    
            }
            //error: AjaxFailed
        });
    

    Showing the xhr.responseText resulted in a very detailed HTML formatted alert message.

提交回复
热议问题