如何获得下一个和之前的jquery getJSON

时间:2014-07-13 07:01:33

标签: javascript jquery json

我想在另一个getJSON中获取getJSON的当前页面?这是正确的吗?在下面的json上获得下一个和上一个的最佳方法是什么。

var currentPage="`http://www.test127.com/ajax/list/?format=json`";
$(document).ready(function(){
  $.getJSON( currentPage, function( data ) {
    $('#next').click(function(){
       currentPage = data.next
       $.getJSON(  data.next, function( data1 ) {
         $.each( data1.results, function(a,b) {
           $( "#change" ).html( "subject:" + " " + b.subject + " " + b.date );
         });
       });
     });
     $('#prev').click(function(){
        $.getJSON(  data.previous, function( data2 ) {
           $.each( data2.results, function(a,b) {
               $( "#change" ).html( "subject:" + " " + b.subject + " " + b.date );
            });
         });
      });
  });
});

data.next是" http://www.test127.com/ajax/list/?page=2&format=json"

{"count": 18, "next": "http://www.test127.com/ajax/list/?page=2&format=json",
 "previous": null, 
 "results": [{"subject": "ronald birthday", 
              "date": "2014-06-13", "time_start": "11:17 PM"},
              {"subject": "ronald birthday", "date": "2014-06-19",
               "time_start": "7:17 PM"}]}

1 个答案:

答案 0 :(得分:0)

你到底想要做什么?

它可能会帮助你理解如何解决它是什么,如果你使用更好的变量名称并以这样的方式编写代码,这样你就不会重复...它更容易阅读和理解。

例如,您可以这样写:

var currentPageUrl="`http://www.test127.com/ajax/list/?format=json`";

function getPageData(url){
  $.getJSON(url, function(data){
    $.each(data.results, function(a,b) {
          $( "#change" ).html( "subject:" + " " + b.subject + " " + b.date );
     });
  });
}

function loadCurrentPage(){
  $.getJSON(currentPageUrl, function( pageData ) {
        $('#next').click(function(){
           currentPageUrl = pageData.next
           getPageData(currentPageUrl);
         });
         $('#prev').click(function(){
           currentPageUrl = pageData.previous
           getPageData(currentPageUrl);
         });
}

$(document).ready(loadCurrentPage);

现在,如果某些事情没有按照你的想法做到,你可能会更容易发现..

如果您对问题有更好的解释,可能会得到更好的帮助。

相关问题