jquery ajax ignores 500 status error

余生长醉 提交于 2019-11-28 10:58:11

It doesn't look like you're using jQuery's document.ready binding correctly. The $().ready(...) version is more-or-less deprecated. Try one of these instead:

$(document).ready(function() {
    $.ajaxSetup({
        error: function(x, e) {
            if (x.status == 500) {
                alert('Internel Server Error.');
            }
        }
    });
});

or the shorthand:

$(function() {
    $.ajaxSetup({
        error: function(x, e) {
            if (x.status == 500) {
                alert('Internel Server Error.');
            }
        }
    });
});

Solved the problem by upgrading jQuery from 1.9.1 to 2.1.1 — now it started calling .error() right on the server response (before it would ignore the 500 response and wait until timeout is over).

Limitation: jQuery 2.1.1 does not support IE8 and below (as IMO shouldn't you)

I had a similar problem, I was using jquery's promise functions of .done, .fail, .always, and if I encountered a 500 internal server error then it would not fire any of the functions (done, fail, always, error). very weird.

in the end I added a timeout into the .ajax options, when it hits the timeout it throws an error and also runs the .fail method.

searchify.myAjaxSearchTerms = $.ajax({
                        'url': url,
                        type: "GET",
                        'dataType': 'jsonp',
                        'jsonp': 'json.wrf',
                        'jsonpCallback': searchify.cbFunc,
                        timeout: 4000, //needed for 500 errors - will go to fail block on timeout
                        beforeSend: searchify.beforeSendAutocomplete

                    });
searchify.myAjaxSearchTerms.fail(function(XHR, status, error){
                        searchify.clearForm();
                        searchify.renderWarningForNoQuery('<div class="notify-bubble"><span class="icon" data-icon="8" aria-hidden="true"></span><span>Sorry. We had a problem performing that search...<br/>Please try again<br/>Enter a <strong>product name</strong>, <strong>catalogue number</strong> or <strong>keyword</strong></span></div>');
                    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!