Let's say I've a code:
$.post("test.php", function(data) {
alert("Data Loaded: " + data);
});
Is there any way to check if the request have failed (eg due to the timeout)?
Yes there is, from the jQuery documentation :
$.post("test.php", function(data) {
alert("Data Loaded: " + data);
})
.fail(function() {
alert("error");
})
Update: drake7077: "error is deprecated as of jquery 1.8, use .fail()"
Two possibilities:
You can register an "ajax error" general callback, which will be called when any ajax operation fails:
$(document).ajaxError(function(event, jqXHR, settings, exception) { ... });
You can fall back to $.ajax()
instead and include your own error handler directly.
edit — @amosrivera is right - the new "Deferred" return values allow for introduction of handlers. Those are available with jQuery 1.5 and newer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.