I have XMLHttpRequest()
function given below
var searchFriendRequests = function (userid) {
var xhr = new XMLHttpRequest();
xhr.open(\'G
When you make your ajax request you can provide dataType option:
dataType: 'json'
For such request when you receive response:
If
json
is specified, the response is parsed usingjQuery.parseJSON
before being passed, as an object, to the success handler. The parsed JSON object is made available through theresponseJSON
property of thejqXHR
object.
you can access your data like:
var data = xhr.responseJSON
Full example:
$ajax.({
dataType: 'json',
success: function( xhr ) {
var yourData = xhr.responseJSON;
console.log( yourData );
},
});
use JSON.parse(), like:
var data=xhr.responseText;
var jsonResponse = JSON.parse(data);
console.log(jsonResponse["Data"]);
For sometime now you can use:
xhr.responseJSON
without any parsing needed. hope it helps
should parse the response to json object first,then get the data field from the response
var responseText = JSON.parse(xhr.responseText),
data = responseText.Data;
You first need to parse responseText in JSON, for this you should use JSON.parse(). Then you can access it using key.
var json = JSON.parse(xhr.responseText);
var yourData = json.Data; // or json["Data"]
To simply get the email, or any other field, from the Data
object, use the following:
data.Data[0].email
WORKING EXAMPLE