views:

361

answers:

5

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.

A: 

THe Message is a return from the actual server side function that you are querying in your ajax call.

That way you can get the error or any other info on whether or not the server side code did what it was supposed to do.

Say if you return a string "success"

msg.val() will equal "success"

hope that helps

sdmiller
A: 

the ajax return variable (in this case 'msg') is the output returned from the AJAX call - in the case of an ajax error, this would probably be a server error.

Eddie
+4  A: 

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 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 check it out, has always been undefined. I don't think it's ever useful.

Patrick Karcher
You're hash tag does not work (unfortunately)
Josh Stodola
A: 

Actually, the correct way is:

error: function(req, status, error) {
}

From the jQuery API:

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.

Per Holmäng
A: 

According to the API (didn't you read this?!), the error function should be defined like this...

error(XMLHttpRequest, textStatus, errorThrown)

And here's what it says about the parameters (notice the bold part)...

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.

Josh Stodola
how do I pass arguments to this function from Web Service?
gnomixa