jquery ajax error – Handling Ajax errors with jQuery Examples

jQuery ajax error function is easy to Handling All Error Like 404 page error, 500 Internal Server error, Requested JSON parse, Time out error, request aborte and error alert message.

jQuery AJAX error and success

We have retrieved the self-made decision from the page anyway we even have simple jquery coded it so it raises an erreo(bug) within the your call page(example url: “response.php”). The ajaxError() method specifies a function to be run when an AJAX request fails. anyway do we get that error from the jquery? how we get jQuery Ajax Error Handling Function hear is the best solution of ajax error function.

jQuery ajax error function – jQuery Ajax Error Handling Function

this simple jQuery ajax error function is very basic as well as easy to use with your ajax call.

$.ajax({
type: "POST",
url: "response.php",
data: { search: '1',keyword : 'pakainfo'},
cache: false,
success: function(response)
{
	// success alert message
	alert(response);
},
error: function (error)
{
	// error alert message
	alert('error :: ' + eval(error));
}
});

jQuery AJAX Example Receiving HTML With AJAX, Receiving JSON With AJAX, Sending Parameters in The AJAX Request, Sending Raw Data in The AJAX Request, Sending JSON in The AJAX Request, HTTP GET and POST, The $.get() and $.post() Functions, The $.getJSON() Function, The load() Function and The $.getScript() Function.

jQuery ajax error function using jqXHR

here jQuery ajax error function using jqXHR in this jquery function i can get different type ajax error like as a 404 page error, 500 Internal Server error, Requested JSON parse, Time out error or many more.

$.ajax({
type: "POST",
url: "response.php",
data: { search: '1',keyword : 'somedata'},
cache: false,
success: function(response)
{
// success alert message
alert(response);
},
error: function (jqXHR, exception) {
var error_msg = '';
if (jqXHR.status === 0) {
error_msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
// 404 page error
error_msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
// 500 Internal Server error
error_msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
// Requested JSON parse
error_msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
// Time out error
error_msg = 'Time out error.';
} else if (exception === 'abort') {
// request aborte
error_msg = 'Ajax request aborted.';
} else {
error_msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
// error alert message
alert('error :: ' + error_msg);
},
});

Parameters for jQuery ajax error function with jqXHR

jQuery ajax error function with an validation error

$.ajax({
type: "POST",
url: "response.php",
data: { search: '1',keyword : 'pakainfo'},
cache: false,
success: function(response, textStatus, jqXHR) {
console.log(response.error);
}
});

Also Read : 500 Internal Server Error Ajax

I hope you get an idea about jquery ajax error.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment