0

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 :)

0

2 Answers 2

2

What you have is a JSON Array. I'm guessing you were looking for something like this:

{
    "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"
        }]
    ]
}

Although I think a better structure would be:

{
    "notifications": [
        {
            "message": "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"
        }
    ]
}

This way notification becomes a property of the object, which means you can access it via data.notifications. Otherwise you'd have to access the notifications via data[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:

<?php
  $array = array(
      "notifications" => array(
          array(
              "message" => "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"
          )
      )
  );

  echo json_encode($array);
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Your PHP response should actually be:

{
    "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"
        }
       ]
    ]
}

Note that with the above, notification is a field inside the object that the string represents. That will allow you to iterate, the way you are doing it with $.each(..).


The way you are doing is it by having an array (note the initial [ and the last ] in the response). The error is because $.each invokes data.notification.length where .length is an operation on undefined.


PHP side code should be somewhat like below:

echo json_encode(array("notifications" => $notifications));

instead of (my guess):

echo json_encode(array("notification", $notifications));

2 Comments

Thank you! Do you know why my json_encode($array) is returning a JSON array rather than object?
thanks for your help! I've figured out why it was giving JSON array - because I wasn't supplying name=>value pairs in array. Used to C and Java arrays :)

Your Answer

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.