JQuery ajax error function

I have ajax data transfer to a page which then returns a value.

I received a successful call from the page, but I encoded it so that it would throw an error in asp. How to get this error from jquery?

For example:

cache: false, url: "addInterview_Code.asp", type: "POST", datatype: "text", data: strData, success: function (html) { alert('successful : ' + html); $("#result").html("Successful"); }, error: function (error) { **alert('error; ' + eval(error));** } 

This is a mistake that I do not understand. What parameter do I need to set in the function so that I can then use the error message that I raised on the server.

+93
jquery ajax
Jul 22 '11 at 16:13
source share
7 answers

The required parameters in the Ajax error function are jqXHR, exception , and you can use it as shown below:

 $.ajax({ url: 'some_unknown_page.html', success: function (response) { $('#post').html(response.responseText); }, error: function (jqXHR, exception) { var msg = ''; if (jqXHR.status === 0) { msg = 'Not connect.\n Verify Network.'; } else if (jqXHR.status == 404) { msg = 'Requested page not found. [404]'; } else if (jqXHR.status == 500) { msg = 'Internal Server Error [500].'; } else if (exception === 'parsererror') { msg = 'Requested JSON parse failed.'; } else if (exception === 'timeout') { msg = 'Time out error.'; } else if (exception === 'abort') { msg = 'Ajax request aborted.'; } else { msg = 'Uncaught Error.\n' + jqXHR.responseText; } $('#post').html(msg); }, }); 

DEMO FIDDLE




Options

jqXHR:

This is actually an error object that looks like

Ajax error jqXHR object

You can also view this in your own browser console using console.log inside the error function, for example:

 error: function (jqXHR, exception) { console.log(jqXHR); // Your error handling logic here.. } 

We use the status property of this object to get an error code, for example, if we get status = 404, this means that the requested page was not found. It does not exist at all. Based on this status code, we can redirect users to the login page or regardless of our business logic.

an exception:

This is a string variable that indicates the type of exception. So, if we get the 404 error, the exception text will just be an β€œerror”. In the same way, we can get a "timeout", "abort" like other exception texts.




Wear notification: jqXHR.success() , jqXHR.error() and jqXHR.complete() are deprecated from jQuery 1.8. To prepare your code for their possible removal, use jqXHR.done() , jqXHR.fail() , and jqXHR.always() .

So, if you are using jQuery 1.8 or higher , we will need to update the logic of the success and error functions, for example: -

 // Assign handlers immediately after making the request, // and remember the jqXHR object for this request var jqxhr = $.ajax("some_unknown_page.html") .done(function (response) { // success logic here $('#post').html(response.responseText); }) .fail(function (jqXHR, exception) { // Our error logic here var msg = ''; if (jqXHR.status === 0) { msg = 'Not connect.\n Verify Network.'; } else if (jqXHR.status == 404) { msg = 'Requested page not found. [404]'; } else if (jqXHR.status == 500) { msg = 'Internal Server Error [500].'; } else if (exception === 'parsererror') { msg = 'Requested JSON parse failed.'; } else if (exception === 'timeout') { msg = 'Time out error.'; } else if (exception === 'abort') { msg = 'Ajax request aborted.'; } else { msg = 'Uncaught Error.\n' + jqXHR.responseText; } $('#post').html(msg); }) .always(function () { alert("complete"); }); 

Hope this helps!

+162
Jan 28 '13 at 13:32
source share

Try this:

 error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus, errorThrown); } 

If you want to tell your front about a validation error, try returning json:

 dataType: 'json', success: function(data, textStatus, jqXHR) { console.log(data.error); } 

Your asp script should return:

 {"error": true} 
+83
Jul 22 '11 at 16:21
source share

This is how you get the asp error.

  cache: false, url: "addInterview_Code.asp", type: "POST", datatype: "text", data: strData, success: function (html) { alert('successful : ' + html); $("#result").html("Successful"); }, error: function (jqXHR, textStatus, errorThrown) { if (jqXHR.status == 500) { alert('Internal error: ' + jqXHR.responseText); } else { alert('Unexpected error.'); } } 
+4
Aug 26 '14 at 18:24
source share
 error(jqXHR, textStatus, errorThrown) 

http://api.jquery.com/jQuery.ajax/

+2
Jul 22 '11 at 16:20
source share
  cache: false, url: "addInterview_Code.asp", type: "POST", datatype: "text", data: strData, success: function (html) { alert('successful : ' + html); $("#result").html("Successful"); }, error: function(data, errorThrown) { alert('request failed :'+errorThrown); } 
+2
Jul 03 '13 at 10:54 on
source share

you use function

 error(error) 

but jquery is actually looking for a function with three parameters:

 error(jqXHR, textStatus, errorThrown) 

you need to add two more parameters.

ALSO: please view all comments above that mention "deprecated" :)

 $.ajax("www.stackoverflow.com/api/whatever", { dataType:"JSON" data: { id=1, name='example' } }).succes(function (result) { // use result }).error(function (jqXHR, textStatus, errorThrown) { // handle error }); 
+2
Jan 27 '17 at 10:14
source share

From jquery.com:

 The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced injQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead. 

If you want to use global handlers, you can use:

 .ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend() 
0
Dec 17 '14 at 18:01
source share



All Articles