php - JSON encoding returns array of objects, how to access them? -


ajax call

$.ajax({     type: 'post',     url: './php/testing/notification-regrab.php',     async: false,     contenttype: 'text/json',     error: function (result) {       alert("error124");     },     success: function (result) {       var data = $.parsejson(result);       console.log(data);     } }); 

result without json.parse

{"notes":{"0":{"id":"3","sender":"0000000011","sendee":"0000000001","sent":"2015-03-11 00:00:00","is_read":"0","type":"14","ix_msg":"you have recived response group invitation!","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"sure, join project group.","target":"1","skill_list":null,"resource_list":null,"response":"1","request_type":null,"request_string":null,"rating":null},"1":{"id":"4","sender":"0000000011","sendee":"0000000001","sent":"2015-03-19 00:00:00","is_read":"0","type":"1","ix_msg":"hey, recieved message mot","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"hey, messgae sent tom mot","target":"0","skill_list":null,"resource_list":null,"response":null,"request_type":null,"request_string":null,"rating":null},"2":{"id":"5","sender":"0000000011","sendee":"0000000001","sent":"2015-03-19 04:13:30","is_read":"0","type":"1","ix_msg":"you have recieved message mot.","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"hey there friend.","target":null,"skill_list":null,"resource_list":null,"response":null,"request_type":null,"request_string":null,"rating":null}}} 

result json.parse (i opened first result object 0, can see holds)

object {notes: object} notes: object   0: object      id: "3"      is_read: "0"      ix_msg: "you have recived response group invitation!"      msg: "sure, join project group."      rating: null      request_string: null      request_type: null      resource_list: null      response: "1"      sendee: "0000000001"      sender: "0000000011"      sender_fname: "mot"      sender_lname: "mot"      sender_username: "mot"      sent: "2015-03-11 00:00:00"      skill_list: null      target: "1"      type: "14"      __proto__: object   1: object   2: object   __proto__: object __proto__: object 

this php array makes above data:

$arr = array(     "id" => $row["id"],     "sender" => $row["sender"],     "sendee" => $row["sendee"],     "type" => $row["type"],     "posting" => $row["posting"],     "msg" => $row["msg"],     "sent" => $row["sent"],     "read" => $row["read"], ); 

problem okay, trying loop through each notification jquery, first want test how can return single notification object array of objects...

i want following example:

console.log(data[0].id);

which should return string "3", since first id in first notification...

if above, says follow error:

uncaught typeerror: cannot read property 'id' of undefined 

i have no idea how read json objects...

please me , tell me how output above data...

first in request change text/kaon application/json make sure server returns proper header.

the php part should like: $note = array("id" => 123, "name" => "some name")

$notes = array(); $notes[] = $note; 

the jquery part easy: $.each(notes, function(i, note) { console.log(note.name); });


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -