0

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
  • 1
    jQuery.each() and/or jQuery.parseJSON(). Commented Jun 22, 2015 at 3:48
  • 2
    alert(json[i]['fname']); Commented Jun 22, 2015 at 3:50

2 Answers 2

1

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

1

It is rather simple to do this:

var data = jQuery.parseJSON(json);
jQuery.each(data, function(i, item) {
 jQuery('.derp').append(item.mname + "<br />");
});

Example


Reference

answered Jun 22, 2015 at 3:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.