I have the following code which detect any error regarding ajax, I need specifically to get "abort" error for ajax.
How to do it?
$(document).bind("ajaxError", function (e, jqXHR, ajaxSettings, thrownError) {
});
I'm not sure if if (thrownError == "abort")
is the correct approach.
Yes you are atmost there see this one
$(function() {
$.ajaxSetup({
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.n' + jqXHR.responseText);
}
}
});
});
For your Reference http://www.sitepoint.com/jquery-ajax-error-handling-function/
EDIT
$(function() {
$.ajaxSetup({
beforeSend: function(jqXHR) {
// Example condition to abort the request
if (someCondition) {
jqXHR.abort();
console.log("AJAX request aborted in beforeSend");
}
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connected.\nVerify Network.');
} else if (jqXHR.status === 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status === 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.'); // This won't trigger if aborted in beforeSend
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
Example AJAX request
$.ajax({
url: "example.php",
type: "GET",
beforeSend: function(jqXHR) {
if (/* some condition to cancel */ false) {
jqXHR.abort();
console.log("Request aborted manually.");
}
},
success: function(response) {
console.log("Success:", response);
}
});
});
Hope it may helps :)