Andrei
Andrei

Reputation: 33

Catch / Handle 502 Bad Gateway Error

I have to update a large collection so I am calling in a loop an web api. I use jQuery.ajax() Something like this:

$.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);
    }
});

The problem is the collection is quite large and sometimes I get a 502 Bad Gateway error and the ajax error handler is not called.

I even tried $( document ).ajaxError() but i'm doing a cross-domain jsonp call , and it seems .ajaxError() does not get called in that situation.

Is there any way to handle that error? Something at window level?
I can see the error in the Chrome development console , and I was thinking there might be a way.

Thanks

Upvotes: 3

Views: 6988

Answers (1)

Matthew Daly
Matthew Daly

Reputation: 9476

Yes, there is: statusCode. See the jQuery documentation on AJAX for details.

Simple example:

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

Upvotes: 9

Related Questions