benhowdle89
benhowdle89

Reputation: 37464

jQuery $.each loop

I have this code:

<script type="text/javascript">
        $(document).ready(function() {
            var url = "https://graph.facebook.com/search?q=cinema&type=post";
            $.ajax({
               type: "POST",
               url: url,
               dataType: "jsonp",
               success: function(msg){
                 console.log( msg );
                 $.each( msg.data , function(obj){
                    $('#cinemas').append(obj.message);
                 });
               }
             });
        });
    </script>

Yet nothing is displayed, can anyone spot anything obviously wrong with this?

Thanks

Upvotes: 2

Views: 786

Answers (4)

TommyMedia
TommyMedia

Reputation: 48

This works for me:

         var g = 0;
            $.each(msg, function(key, value) {
             $('#cinemas').append(msg["data"][g]["message"]);

            g++;
        });

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227230

You need to add callback=? for 'JSONP' to work correctly.

var url = "https://graph.facebook.com/search?q=cinema&type=post&callback=?";

EDIT: In $.each the callback is function(index, value), so you need to use the 2nd param.

$.each( msg.data , function(i, obj){
  $('#cinemas').append(obj.message);
});

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77966

$.each needs (index, value)

$(document).ready(function() {
    var url = "https://graph.facebook.com/search?q=cinema&type=post";
    $.ajax({
       type: "POST",
       url: url,
       dataType: "jsonp",
       success: function(msg){
         $.each( msg.data , function(i, obj){
                $('#cinemas').append(obj.message);
         });
       }
     });
});

http://jsfiddle.net/AlienWebguy/q8b7Q/1/

Upvotes: 2

user113716
user113716

Reputation: 322492

You're using the wrong argument in $.each.

  // Use the 2nd argument---------v
 $.each( msg.data , function( i, obj ){
    $('#cinemas').append(obj.message);
 });

Upvotes: 1

Related Questions