Show proper error messages with jQuery AJAX

后端 未结 2 770
南旧
南旧 2021-02-06 05:03

Currently if I have an error with an ajax request such as submitting a form (I\'m using ASP.NET MVC 3) it will show something like \'Internal Server Error\'.

But if I do

2条回答
  •  天涯浪人
    2021-02-06 05:36

    This code will replace the current page's contents with the error that came back from the ajax request.

    jQuery(document).ajaxError(function (event, request, settings) {
        if (request.status === 500) {
            $(document.body).html(request.responseText);
        } else if (request.status === 401) {
            // redirect to login, 401 means session is expired.
            window.location.href = "login page url";
        }
    });
    

提交回复
热议问题