jQuery ajax error function – jQuery Ajax Error Handling Function

In this post we will show you how to jQuery ajax error function when Ajax decision passing information to a page that then returns a worth(value).

I have retrieved the self-made decision from the page however I even have coded it so it raises an erreo(bug) within the your call page(example url: "data.php"). however do we retrieve that error from the jquery? how we get jQuery Ajax Error Handling Function hear is the solution of ajax error function.

this jQuery ajax error function is very basic and easy to use with your ajax call.

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

jQuery ajax error function using jqXHR

jQuery ajax error function using jqXHR in this function we can get different type ajax error like 404 page error, 500 Internal Server error, Requested JSON parse, Time out error.

$.ajax({
	type: "POST",
	url: "data.php",
	data: { search: '1',keyword : 'somedata'},
	cache: false,
	success: function(data)
	{               
		// success alert message
	    alert(data);
	},
	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

Parameters for jQuery ajax error function with jqXHR for It actually an error object which is looks like this.

jQuery ajax error function with an validation error

jQuery ajax error function with an validation error :: If we want to inform(get error message) in frontend about an validation error try this method.

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