Articles
jQuery Ajax Error Handling Function
Have you ever wondered what went wrong when working on jQuery Ajax operations? It could be a painful experience to try to find out the bugs or the root of the problem when you don’t know what exactly is the problem. In this article, I am going to share with you a simple way of using Ajax Error Handling Function in jQuery.
Examples of using Ajax Error Handling Function in jQuery
Consider the following example code below:
$.ajax({ type: "POST", url: "ajax_page.php", success: function(data, status, xhr) { // perform operations on success alert('Success'); }, error: function(xhr, status, error) { // perform operations on error alert(xhr.responseText); } });
In the above code, note the usage of “xhr.responseText”. We are alerting (echoing) the error we receive in the jQuery Ajax operations. Just comment it out or remove it, when moving the files to production.
That’s it!
Do you know of any other ways to write jQuery Ajax Error Handling Function? Feel free to share by commenting below.