34

The documentation indicates that the error: option function will make available: XHR instance, a status message string (in this case always error) and an optional exception object returned from the XHR instance (Book: JQuery in Action)

Using the following (in the $.ajax call) I was able to determine I had a "parsererror" and a "timeout" (since I added the timeout: option) error

error: function(request, error){}

What are other things you evaluate in the error option? do you include the optional exception object?

EDIT: one of the answers indicates all the return errors...learning more about what is of value (for debugging) in the XHR instance and exception object would be helpful

This is a complete $.ajax call:

$.ajax({
 type: "post",
 url: "http://myServer/cgi-bin/broker" ,
 dataType: "text",
 data: {
 '_service' : 'myService',
 '_program' : 'myProgram',
 'start' : start,
 'end' : end
 },
 beforeSend: function() {
  $("#loading").removeClass("hide");
 },
 timeout: 5000,
 error: function(request,error) {
  $("#loading").addClass("hide");
  if (error == "timeout") {
   $("#error").append("The request timed out, please resubmit");
  }
  else {
   $("#error").append("ERROR: " + error);
  }
  },
  success: function(request) {
   $("#loading").addClass("hide");
   var t = eval( "(" + request + ")" ) ;
  } // End success
}); // End ajax method

Thanks for the input

5 Answers 5

43

I find the request more useful than the error.

error:function(xhr,err){
    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
    alert("responseText: "+xhr.responseText);
}

xhr is XmlHttpRequest.
readyState values are 1:loading, 2:loaded, 3:interactive, 4:complete.
status is the HTTP status number, i.e. 404: not found, 500: server error, 200: ok.
responseText is the response from the server - this could be text or JSON from the web service, or HTML from the web server.

Sign up to request clarification or add additional context in comments.

Comments

22

Looking at the jQuery source code, there are four returned statuses, in additon to success:

  • timeout - when your specified timeout is exceeded
  • error - http error, like 404
  • notmodified - when requested resource was not modified since last request
  • parsererror - when an xml/json response is bad

1 Comment

also when dataType: "json" and the json is incorrectly formed
4

This is an aside, but I think there's a bug in the code you submitted. The line:

 if (error = "timeout") {

should have more equals signs in it:

 if (error == "timeout") {

1 Comment

good catch, edited the post. It has been a while since I posted this, but I think that was just a typo. thanks for the response
1

The second argument that is passed to your error function will either be the string "timeout" "parserror" "error" or "notmodified". The third will be the exception object. This object can be helpful for debugging.

1 Comment

could you update your answer to explain the helpful debug info that can be obtained from the exception object
0

Are you sure that response is correct? Parse error mean that there is sth wrong with data being evaluted in line var t = eval( "(" + request + ")" ) ;

1 Comment

sorry, my question wasn't clear. I worked through my "parsererror" problems...I was wondering what other info can be determined from the error: option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.