0

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 a response to your group invitation!","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"Sure, I will join the 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, you recieved a message from mot","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"Hey, This is a messgae sent to tom from 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 a message from 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 WITH json.parse (I opened the first result for Object 0, so you can see what it holds)

Object {notes: Object}
notes: Object
  0: Object
     id: "3"
     is_read: "0"
     ix_msg: "You have recived a response to your group invitation!"
     msg: "Sure, I will join the 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 is the PHP array that makes up the 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, so I am trying to loop through each notification with jquery, but I first want to test how I can return a single notification object from this array of objects...

I want to do the following for example:

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

Which should just return the string "3", since that is the first id in the first notification...

If I do the above, it says the follow error:

Uncaught TypeError: Cannot read property 'id' of undefined

I have no idea how to read the json objects...

Please someone help me and tell me how to properly output the above data...

2
  • 1
    FYI: That's not an array of objects.
    – Ram
    Mar 28, 2015 at 22:28
  • why is this getting upvoted?
    – Ryan
    Mar 28, 2015 at 22:30

2 Answers 2

1

First in the request change text/kaon with application/json also make sure the server returns the proper header.

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

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

The jQuery part is easy: $.each(notes, function(i, note) { console.log(note.name); });

0

I figured it out....

I had this before, but I forgot to use parseJSON...

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

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.