2015-07-19 28 views
0

我想使用ajax get方法從頁面中獲取3個數據。如何在分頁中使用ajax獲取數據?

的JavaScript是

<script type="text/javascript"> 
    var busy = false; 
    var limit = 15 
    var offset = 0; 
    var id = 150; 

    function displayRecords(lim, off) { 
    $.ajax({ 
     type: "GET", 
     async: false, 
     url: "s.php", 
     data: "limit=" + lim + "&offset=" + off, 
     cache: false, 
     beforeSend: function() { 
     $("#loader_message").html("").hide(); 
     $('#loader_image').show(); 
     }, 
     success: function(html) { 
     $("#results").append(html); 
     $('#loader_image').hide(); 
     if (html == "") { 
      $("#loader_message").html('<button class="btn btn-default" type="button">No more records.</button>').show() 
     } else { 
      $("#loader_message").html('<button class="btn btn-default" type="button">Loading please wait...</button>').show(); 
     } 
     window.busy = false; 


     } 
    }); 
    } 

    $(document).ready(function() { 
    // start to load the first set of data 
    if (busy == false) { 
     busy = true; 
     // start to load the first set of data 
     displayRecords(limit, offset); 
    } 

    $(window).scroll(function() { 
     // make sure u give the container id of the data to be loaded in. 
     if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) { 
     busy = true; 
     offset = limit + offset; 
     // this is optional just to delay the loading of data 
     setTimeout(function() { displayRecords(limit, offset); }, 500); 
     // you can remove the above code and can use directly this function 
     // displayRecords(limit, offset); 
     } 
    }); 

    }); 

</script> 

和s.php是

$limit = (intval($_GET['limit']) != 0) ? $_GET['limit'] : 10; 
$offset = (intval($_GET['offset']) != 0) ? $_GET['offset'] : 0; 
$id = $_GET['id']; 
echo $id; 

,但我不能讓id的值,每次我想要新的方式出現了問題,或者它顯示錯誤,php文件不會使用ajax獲取id值。但它會完美地獲得極限值和偏移值。限制和偏移量不是一個常數值,但id是恆定值。

我該如何解決這個問題..?

+0

通過傳遞id來獲取=>'數據: 「限制=」 + LIM + 「&偏移量=」 + off +「&id = yourIDName」,' – lshettyl

+0

我改變了我的代碼,關於你的建議,如'data:「limit =」+ lim +「&offset =」+ off +「&id = id」,'echo $ id作爲ididididididididid不是10,我可以核心這個? –

回答

1

您無法獲取$ id的值,因爲它不是在ajax請求中發送的。

data: "limit=" + lim + "&offset=" + off + "&id=" + id, 

您可能想要將id添加到數據字符串。它現在應該工作。

0

試一下:

data: limit: lim, offset: off, id: id 
+0

它顯示結果1010101010101010而不是10 –

0

id是一個變量,你需要時間交給請求像下面則params的一個。

data: { 
    'limit': lim, 
    'offset': off, 
    'id': id 
} 

避免手工編碼的查詢字符串爲jQuery自動會替你下面這樣的:通過

data: "limit=" + lim + "&offset=" + off + "&id=" + id 
相關問題