Currently working for the first time with JSON and with little experience of jQuery.
I have this function that gets triggered on “success” of $.ajax request:
function(data) {
$.each(data.notifications, function(notifications) {
alert('New Notification!');
});
}
However I get an error in the firebug console stating “object is undefined” “length = object.length”.
The JSON response is:
["notifications",[["test would like to connect with you",{"Accept":"\/events\/index.php\/user\/connection?userId=20625101&action=accept","Decline":"\/events\/index.php\/user\/connection?userId=20625101&action=decline"}]]]
I guess it has something to do with the number of []s but the JSON was encoded by PHP using json_encode()
Any help would be appreciated!
Thanks 🙂
What you have is a JSON Array. I’m guessing you were looking for something like this:
Although I think a better structure would be:
This way
notificationbecomes a property of the object, which means you can access it viadata.notifications. Otherwise you’d have to access the notifications viadata[1](data[0]would contain the string “notifications” which essentially becomes meaningless).The following example should give you an idea as far as setting up your data in PHP: