ajax函数没有返回正确的数据

时间:2014-04-19 23:33:52

标签: javascript php jquery ajax pagination

我正在尝试从这个ajax函数中获取正确的信息(缩略图,图像的所有者)。我不认为它知道data.images [i] .imgurl和data.images [i] .username是什么。

ajax.php

  require_once 'instagram.class.php';

  // Initialize class for public requests
  $instagram = new Instagram('123456');

  // Receive AJAX request and create call object
  $tag = $_GET['tag'];
  $maxID = $_GET['max_id'];
  $clientID = $instagram->getApiKey();

  $call = new stdClass;
  $call->pagination->next_max_id = $maxID;
  $call->pagination->next_url = "https://api.instagram.com/v1/tags/{$tag}/media/recent?client_id={$clientID}&max_tag_id={$maxID}";

  // Receive new data
  $media = $instagram->getTagMedia($tag,$auth=false,array('max_tag_id'=>$maxID));

  // Collect everything for json output
  $images = array();

  foreach ($media->data as $data) {
 $images[] = array(
           "imgurl"=>$data->images->thumbnail->url,
        "username"=>$data->user->username;
        );
    }

  echo json_encode(array(
    'next_id' => $media->pagination->next_max_id,
    'images'  => $images,

  ));

的search.php

<script>
   $(document).ready(function() {
    $('#more').click(function() {
    var tag   = $(this).data('tag'),
        maxid = $(this).data('maxid'),


    $.ajax({
      type: 'GET',
      url: 'ajax.php',
      data: {
        tag: tag,
        max_id: maxid

      },
      dataType: 'json',
      cache: false,
      success: function(data) {
        // Output data
        $.each(data.images, function(i, src) {

          $('#photos').append('<div id=box><div class=mainimg><img src="'+  data.images[i].imgurl +'"></div><div class=\"pfooter\">'+ data.images[i].username +'</div></div>');
        });

        // Store new maxid
        $('#more').data('maxid', data.next_id);
      }
    });
  });
});

...

1 个答案:

答案 0 :(得分:2)

search.php文件中,您必须用分号关闭变量语句。

更改此行:

var tag   = $(this).data('tag'),
    maxid = $(this).data('maxid'),

到此:

var tag   = $(this).data('tag'),
    maxid = $(this).data('maxid'); // <-- added semicolon