Reputation: 21667
How would I be able to get the username in jquery from the following
{"id":true,"username":"mynameisdonald"}
I have tried console.log(data); which shows the above and when i use console.log(data.username) it shows as undefined
Upvotes: 0
Views: 59
Reputation: 39550
In your jQuery call, specify that this is JSON:
$.post(
"backend.php",
data: "nodata",
function(data) {
console.log(data.username);
},
"json"
);
or use jQuery's parseJSON
like so:
$.post(
"backend.php",
data: "nodata",
function(rawData) {
var data = $.parseJSON(rawData);
console.log(data.username);
}
);
Upvotes: 1