捕获/处理502错误的网关错误

时间:2014-02-24 12:06:32

标签: javascript jquery ajax

我必须更新一个大型集合,所以我在循环中调用web api。我用jQuery.ajax() 像这样:

$.ajax({
   type: 'GET',
    url: 'http://www.somesite.com/API/API.php',
    jsonpCallback: 'API_SC4',
    contentType: "application/json",
    dataType: 'jsonp',
    data:'action=update&page='+collection[currentIndex].name+'&callback=API_SC4',
    async:false,
    success: function(data) {

        //use data for update of collection[currentIndex]

        UpdateNext(currentIndex+1);
    },
    error: function(e) {
       //interpret error
       UpdateNext(currentIndex+1);
    }
});

问题是集合非常大,有时我得到502 Bad Gateway错误,并且没有调用ajax错误处理程序。

我甚至试过$( document ).ajaxError()但我正在进行跨域jsonp调用,看起来.ajaxError()在这种情况下不会被调用。

有没有办法处理这个错误?在窗口水平的东西?
我可以在Chrome开发控制台中看到错误,我认为可能有办法。

由于

1 个答案:

答案 0 :(得分:7)

是的,有:statusCode。请参阅the jQuery documentation on AJAX for details

简单示例:

$.ajax({
    statusCode: {
        502: function () {
            alert('Fail!');
        }
    }
});