Access the json response from xhr in jquery

I have a question about how the XMLHttpRequest object works. In https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest, under properties I am looking at the "response" property. The description says I should get a JavaScript object back if my content type is json. However, in my code the xhr.response property is undefined.

Specifically, I have a .ajaxSuccess() callback that is setup on a DOM element, it receives the parameters (event, xhr, settings). xhr.responseText is the plaintext version of my json, but xhr.response is undefined. Is there any way I can get the object from xhr without calling $.parseJson(xhr.responseText)?

Answers


I think you are confusing the XMLHttpRequest object with jQuery's jqXHR object used by $.ajaxSuccess(), which is supposed to be a superset of the former.

Instead, you could use $.ajax()'s done handler, which will parse the JSON for you if the data type is json. For example:

$.ajax({
  url: url,
  dataType: 'json',
}).done(function (response) {
  // response is a JSON object, previously parsed by jQuery using $.parseJSON
});

Also note that even though the documentation of $.ajaxSuccess() says that you get a vanilla XMLHttpRequest object, that is no longer true as of jQuery 1.4.1. Instead, you get a jqXHR object.


Need Your Help

Why doesn't AVPlayer stop loading data?

ios ios7 avplayer

When I watch my mac's network connectivity I can tell that a movie is still buffering even after I remove the view which had the AVPlayer on it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.