I want to post some data to php function by ajax, then get the encoded json object that the php function will return, then I want to get the information (keys and values) from this object, but I don't know how, here is my code:
$.ajax({
url: "functions.php",
dataType: "JSON",
data: {id: id},
type: 'POST',
success: function(json){
for(var i=0;i<json.length;i++){
alert(json['fname']);
}
}
});
and here is the json object returned:
[{"id":"1","fname":"kjhkj","mname":"kjhjh","lname":"lname","prefix":"Mr.","suffix":"jhkjhk","email":"[email protected]","image":"11281454_423648214427141_318277024_o.jpg","info":"hjgvhd"}]
Bjoern
16.3k4 gold badges47 silver badges50 bronze badges
asked Jun 22, 2015 at 3:44
2 Answers 2
Try:
$.ajax({
url: "functions.php",
dataType: "JSON",
data: {id: id},
type: 'POST',
success: function(json){
for(var i=0;i<json.length;i++){
alert(json[i].fname);
}
}
});
answered Jun 22, 2015 at 3:54
Sign up to request clarification or add additional context in comments.
Comments
It is rather simple to do this:
var data = jQuery.parseJSON(json);
jQuery.each(data, function(i, item) {
jQuery('.derp').append(item.mname + "<br />");
});
Reference
answered Jun 22, 2015 at 3:50
Comments
default
jQuery.each()
and/orjQuery.parseJSON()
.alert(json[i]['fname']);