0
我想在我的項目中實現無限滾動。我創建了3頁index.php,ajax.php和script.js。索引工作正常。 Ajax.php從mysql數據庫以json格式返回數據。使用Ajax加載json數據(未捕獲TypeError:無法讀取未定義的屬性'長度')
問題與script.js 以下是代碼。
的script.js:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()){
first = $('#first').val();
limit = $('#limit').val();
no_data = true;
if(flag && no_data){
flag = false;
$('#loader').show();
$.ajax({
url : 'ajax.php',
dataType: "json",
type: 'post',
data: {
start : first,
limit : limit
},
success: function(data) {
flag = true;
$('#loader').hide();
if(data.count > 0){
first = parseInt($('#first').val());
limit = parseInt($('#limit').val());
$('#first').val(first+limit);
$("#jc").append("<p>Test</p>");
//$('#jc').append();
$.each(data.content, function(key, value){
alert(value.name);
//$("#jc").append("<p>Test1</p>");
html +=value.name;
html += '<span class="month"><i class="fa fa-calendar"></i>'+value.name+'</span><p> </p>';
html += '<p><a href="'+value.email+'" target="_blank">Demo </a></p>';
html += '<p><a href="'+value.job_description+'" target="_blank">Tutorial </a></p>';
html += '<p>'+value.mobile+'</p>';
html += '</li>';
$('#jc').append(html);
});
}else{
alert('No more data to show');
no_data = false;
}
},
error: function(data){
flag = true;
$('#loader').hide();
no_data = false;
alert('Something went wrong, Please contact admin');
}
});
}
}
});
好像
`$.each(data.content, function(key, value){`
問題與此線,因爲這樣我把警告標籤之後,但不點火。
JSON數據
{"count":2,"0":{"name":" Admin","email":"[email protected]","description":"We are looking json data. ","mobile":"123456789","newDate":"01-Apr-2016"},"1":{"name":"Raja","email":"[email protected]","description":"testing testing ","mobile":"43455435422","newDate":"31-Mar-2016"}}
我在控制檯檢查,發現這個錯誤。 jquery-2.1.3.min.js:2 Uncaught TypeError: Cannot read property 'length' of undefined
謝謝您的回答。註冊了 – Ironic
我們也可以使用hasOwnProperty,'value.hasOwnProperty('name')'來檢查它。 – sahil
它的工作,但我得到一個額外的JSON數據爲未定義。像json從數據庫中返回2條記錄,但是當我將它們打印爲html時,我得到了額外的記錄(udefined)。 – Ironic