简体   繁体   English

Instagram API的Ajax分页尝试修复bug

[英]Ajax pagination for Instagram API tried to fix bug

I'm trying to fix Ajax pagination for Instagram API 我正在尝试修复Instagram API的Ajax分页

Shown only 20 photos. 仅显示了20张照片。 "Load more" button didn't work. “加载更多”按钮不起作用。

In console: 在控制台中:

Uncaught ReferenceError: maxid is not defined

Here is index.php: 这是index.php:

 <script>
 $(document).ready(function() {
 $('#more').click(function() {
 var max_id = $(this).data('nextid');


 $.ajax({
 type: 'GET',
 url: 'ajax.php',
 data: {
 max_id: maxid
 },
 dataType: 'json',
 cache: false,
 success: function(data) {
 // Output data
 $.each(data.images, function(i, src) {
 $('#photos').append('<li><img src="' + src + '"></li>');
 });

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

<?php


 require_once 'instagram.class.php';


$instagram = new Instagram(array(
  'apiKey' => '*****',
  'apiSecret' => '*****',
  'apiCallback' => '******'

));

// Receive OAuth code parameter
$code = $_GET['code'];

if (true === isset($code)) {

$data = $instagram->getOAuthToken($code);

$instagram->setAccessToken($data);


$media = $instagram->getUserMedia();


}
?>


<ul id="photos">

<?php foreach( $media->data as $data ): ?>
<li><img src="<?php echo $data->images->thumbnail->url ?>"></li>
<?php endforeach; ?>

<?php echo "<br><button id=\"more\" data-maxid=\"{$media->pagination->next_max_id}\">Load more ...</button>"; ?>

ajax.php: ajax.php:

require_once 'instagram.class.php';
$instagram = new Instagram(array(
  'apiKey' => '****',
  'apiSecret' => '*****',
  'apiCallback' => '*******'

));

// Receive OAuth code parameter
$code = $_GET['code'];

if (true === isset($code)) {

$data = $instagram->getOAuthToken($code);

 // Initialize class for public requests
 $instagram->setAccessToken($data);

$media = $instagram->getUserMedia();


 // Receive AJAX request and create call object
 $maxID = $_GET['next_max_id'];

 $clientID = $instagram->getApiKey();

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


 // Receive new data
 $media = $instagram->pagination($call);

 // Collect everything for json output
 $images = array();
 foreach ($media->data as $data) {
 $images[] = $data->images->thumbnail->url;
  }
 echo json_encode(array(
 'next_id' => $media->pagination->next_max_id,
'max_id' => $media->pagination->max_id,
 'images' => $images
 ));

I am using https://github.com/cosenary/Instagram-PHP-API 我正在使用https://github.com/cosenary/Instagram-PHP-API

Thank You! 谢谢!

How can it be clearer than: 如何更清楚:

Uncaught ReferenceError: maxid is not defined

maxid is not defined in your code. maxid未在您的代码中定义。 What is defined is max_id in this line: 在这一行中定义的是max_id

var max_id = $(this).data('nextid');

There are just 2 locations with maxid : maxid只有2个位置:

Here: 这里:

$.ajax({ $就({
... ...
max_id: maxid max_id:maxid
}, },

and here: 和这里:

// Store new maxid //存储新的maxid
$('#more').data('maxid', data.next_id); $('#more')。data('maxid',data.next_id);

When you replace these occurences with your defined max_id (thanks to @mathieu) it should be fixed. 当您使用定义的max_id替换这些出现时(感谢@mathieu),应该修复它。

In your ajax.php you have a line: 在你的ajax.php你有一条线:

$maxID = $_GET['next_max_id']; $ maxID = $ _GET ['next_max_id'];

This doesn't correspond to your call above, where you have a max_id . 这与您上面的调用不对应,您有一个max_id Seems like you mixed some next/max/id . 好像你混合了一些next/max/id If you look through your code and clean up these ids, it should work. 如果您查看代码并清理这些ID,它应该可以正常工作。

It looks like 看起来像

.data('nextid');

is a careless error as that doesn't exist in your DOM. 是一个粗心的错误,因为你的DOM中不存在这个错误。 Unless it's somewhere else and you haven't posted it. 除非它在其他地方,你没有发布它。

Did you mean to have 你的意思是

.data('maxid');

?

You need to pass the event argument with the click and get the data from event.target . 您需要通过单击传递event参数并从event.target获取data
Change the first lines of codes into this: 将第一行代码更改为:

$('#more').click(function( event ) {
var max_id = $(event.target).data('nextid');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM