What are the returned parameters in the jQuery ajax success option?

jQuery documentation indicates the following is returned:

success(data, textStatus, XMLHttpRequest)Function

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the 'dataType' parameter; a string describing the status; and the XMLHttpRequest object. This is an Ajax Event.

I have only been using the "data" parameter. What is the purpose of the "textStatus"?...I get "undefined" when I put this var in an alert (within the success function) What can I get from the XMLHttpRequest? and how do I access it?

Thanks


You could use the XMLHttpRequest object to get any response headers the server sent. This is of limited usefulness for services you control as it's usually better to pass back all information in the JSON or XML of the response body, but it could be useful for talking to services whose interfaces you don't have control over, that put useful information in the headers.

The textStatus can be of use for an error-callback to tell you how far the request got. But for success it would only tell you whether the server returned a 200 OK response (in which case you should get 'success' ) or whether you got a 304 Not Modified and the body returned from browser cache instead (in which case it should be 'notmodified' . This is highly unlikely to be useful.


According to this, possible values for textStatus are:

"timeout"
"error"
"notmodified"
"success"
"parsererror"

I believe if you're in the success callback, you will only ever see the "success" textStatus. Not sure why you would see undefined .

The XMLHttpRequest contains lower level information that you usually won't need, like the response state and parameters used to make the Ajax request. Usually all you're concerned with is the data parameter, which is derived from XMLHttpRequest object itself. You can find the rest of the methods and properties of XMLHttpRequest at this page.


The testStatus refers to AJAX readyStates, which you can read about here. I am not quite sure why it is coming across as undefined. I am not 100% used to AJAX with jQuery, however during a hand crafted ajax call, your return function will be called a number of times, with varying status codes.

The XMLHttpRequest gives you back the actual object which is making the asynchronous call.

链接地址: http://www.djcxy.com/p/39202.html

上一篇: 我怎样才能在ASP.NET MVC的JSON格式返回500错误?

下一篇: jQuery ajax成功选项中返回的参数是什么?