我在我的主頁中有以下代碼,試圖拉AJAX JSON。 它只需在每個頁面上滾動顯示10個額外的產品。目前它只顯示加載後應該刪除的加載程序Circle div - 但沒有加載。Ajax JSON不返回
var handler = null;
var page = 1;
var isLoading = false;
var apiURL = 'ajax/api.php'
function onScroll(event) {
// Only check when we're not still waiting for data.
if(!isLoading) {
// Check if we're within 100 pixels of the bottom edge of the broser window.
var closeToBottom = ($(window).scrollTop() + $(window).height() >
$(document).height() - 100);
if(closeToBottom) {
loadData();
}
}
};
function loadData() {
isLoading = true;
$('#loaderCircle').show();
$.ajax({
url: apiURL,
dataType: 'jsonp',
data: {page: page}, // Page parameter to make sure we load new data
success: onLoadData
});
};
// Receives data from the API, creates HTML for images
function onLoadData(data) {
isLoading = false;
$('#loaderCircle').hide();
// Increment page index for future calls.
page++;
// Create HTML for the images.
var html = '';
var i=0, length=data.length, image;
for(; i<length; i++) {
image = data[i];
html += '<li>';
// Image tag
html += '<img src="products/200/'+p_id+'.jpg" ">';
// Image title.
html += '<p>'+p_name+'</p>';
html += '</li>';
}
// Add image HTML to the page.
$('#tiles').append(html);
};
我的PHP JSON調用
<?php require_once('../inc/config.php');
$page = $_REQUEST['page'];
$items = '10';
$start = (($page * $items) - $items);
$end = ($page * $items);
$result = mysql_query("SELECT p_id, p_name FROM products ORDER BY p_id ASC LIMIT $start, $end");
$products = array();
while($product = mysql_fetch_array($result, MYSQL_ASSOC)) {
$products[] = ($product);
}
$output = json_encode($products);
echo $output;
?>
的PHP顯示器是作爲JSON如下(例如數據):
[{"p_id":"1","p_name":"ASOS Pleated Swing Mac"},{"p_id":"2","p_name":"ASOS Midi Belted Coat"},{"p_id":"3","p_name":"ASOS Zig Zag Coat"},{"p_id":"4","p_name":"Collarless Quilted Leather Biker with Ribbed Sleeve"},{"p_id":"6","p_name":"TFNC Dress with Wrap Front in Flocked Heart"},{"p_id":"7","p_name":"Striped Skater Dress"},
{"p_id":"8","p_name":"Metallic Wrap Dress"},{"p_id":"9","p_name":"Strapless Dress With Neon Belt"},{"p_id":"10","p_name":"Darling Floral Border Print Dress"},{"p_id":"11","p_name":"Dip Hem Chiffon Dress With Printed Top"}]
所以,總體來說它不將數據加載到HTML 。有人能解釋我可能要做什麼或者我可能做錯了什麼。 (這是使用wookmark插件 - 但不應該影響什麼)
嘗試增加()的錯誤處理程序,以你的Ajax功能,並看看它是否被解僱,以及發生了什麼錯誤。並確保您返回的數據實際上是jsonp,而不是常規的json。 – adeneo 2012-04-29 01:12:40
錯誤提供了200.和錯誤:jQuery1720435445470206538_1335662563183未被調用。我猜是因爲它正在發送其他參數。 – Anthony1234 2012-04-29 01:23:21
而且我如何確保這是真正的JSONP? – Anthony1234 2012-04-29 01:27:25