Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.ajax() function's error message source

Tags:

In the case of failure, the code goes something like:

error: function(msg) 

where does the msg come from?

EDIT:

I am using this function ($ajax) to call a WEB SERVICE. So if whoever voted this down could explain to me where msg comes from that would be great! Do I set it in web service? If so, how? Please don't copy and paste the definitions.

like image 784
sarsnake Avatar asked Jan 21 '10 22:01

sarsnake


People also ask

How can we handle exception handling in Ajax?

Example: We are going to see how to use AJAX fail() methods to handle the error in the HTTP requests. The fail() callback takes 3 parameters where the first parameter is a JSON error object, the second parameter is given a reason in text format and the last parameter is for the error thrown by the HTTP request.

What triggers Ajax error?

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.


2 Answers

From the jquery documentation:

error(XMLHttpRequest, textStatus, errorThrown) Function

A function to be called if the request fails. The function is passed three arguments:
- The XMLHttpRequest object,
- a string describing the type of error that occurred, and
- an optional exception object, if one occurred.
Possible values for the second argument (besides null) are "timeout", "error", "notmodified" and "parsererror". This is an Ajax Event.

When you indicate just one parameter, it will be the xmlHttpRequestObject. You can get some good information from that. Sadly, most documentation out there doesn't have you set this up correctly. A good default template is:

error:function (xhRequest, errorText, thrownError) 

Some good info you can get from xhRequest are:

  • .status : 404: "not found", 500: "server error". This can sometimes be a big help.
  • .responseText is information from the server, often useless in the case of an error but sometimes can be helpful.

The second value, a string, is sometimes helpful. Ah, I guess the possible values are mentioned in the documentation.

The 3rd parameter, whenever I checked it out, has always been undefined. I don't think it's ever useful.

like image 114
Patrick Karcher Avatar answered Sep 19 '22 00:09

Patrick Karcher


I think the 3rd parameter errorThrown in the error callback function

 error(XMLHttpRequest, textStatus, errorThrown) 

is exactly to send the text error message from server.

So if on a server you set:

 Response.Status = 403;  Response.StatusDescription = "Password is not correct"; 

on a client you get:

 textStatus => "error",  errorThrown => "Password is not correct" 

Server part for Asp.net MVC will be:

 return new HttpStatusCodeResult(403, "Password is not correct"); 
like image 41
Vladimir Buskin Avatar answered Sep 22 '22 00:09

Vladimir Buskin