javascript 如何使用 jquery 捕获 500 错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13639225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:20:52  来源:igfitidea点击:

How to capture the 500 error message using jquery?

javascriptjqueryajax

提问by user244394

How can i capture the 500 error message using jquery? I want to keep on checking for the 500 error message for sometime until it changes and time out after 50 sec.

如何使用 jquery 捕获 500 错误消息?我想继续检查 500 错误消息一段时间,直到它更改并在 50 秒后超时。

I used the code below to try to capture and check the 500 error message but it doesnt seem to catch the 500 error message. I can see it in the firebug

我使用下面的代码尝试捕获并检查 500 错误消息,但似乎没有捕获 500 错误消息。我可以在萤火虫中看到它

$.ajax({
    statusCode: {
        500: function() {
            alert(" 500 data still loading");
            console.log('500 ');
        }
    }
}); 

采纳答案by Murali Prasanth

Are you missing urlin $.ajaxlike the one below

是否缺少url$.ajax类似下面

$.ajax({
    url: "/path to page",
    statusCode: {
        500: function() {
            alert(" 500 data still loading");
            console.log('500 ');
        }
    }
}); 

回答by Vicary

Dispite the accepted answer mentioned by @Danny, you can also do this in newer versions of jQuery.

尽管@Danny 提到了已接受的答案,但您也可以在较新版本的 jQuery 中执行此操作。

var xhr = $.ajax({
    url: "somewhere"
});

xhr.fail(function(xhr, textStatus, error) {
    // Error handling stuff here ...
});

See Deferred Object.

请参阅延迟对象

回答by Sridhar Narasimhan

You can check the status in error of ajax post please check the below code.

您可以检查ajax帖子的错误状态,请检查以下代码。

 $.ajax({
        .....
        success: function (data) {

        },
        complete: function (XMLHttpRequest, textStatus) {

        },
        error: function (e, status) {

            if (e.status == 404)
                alert("404 error");
        }
    });

Thanks

谢谢