diank diank - 5 months ago 8
Ajax Question

jQuery AJAX multiple calls error handling

I try to handling errors. Here is my case - I have multiple AJAX calls from different urls. I want when some url response error 500 to know which url is. Here is my code.

var urls = ["url1.php", "url2.php", "url3.php"];
$.each(urls, function(index, value) {
$.ajax({
global: false,
type: 'POST',
url: value,
dataType: 'html',
data: returnData(),
success: function(result) {
switch(value) {
case "url1.php":
break;
case "url2.php":
break;
case "url3.php":
break;
}
},
error: function (e, request, status, error) {
if (e.status == 500) {
alert("500 error");
}
}
});
});


Now when one of them return error 500 I cannot know which url is.
I will be very grateful if someone give advice. Thanks in advance.

Answer

You should have it like this:

        error: function (e, request, status, error) {
            if (e.status == 500) {
                alert("500 error" + $(this)[0].url);
            }
        }

You might need to debug to make sure of $(this)[0].url part, I was just checking where to find the url in the error callback in my code somewhere, it might not be the same for you? I am not sure. But you should be able to find the url in $(this) object.

Comments