我在Google Chrome控制台上的IDE中出现错误,指出:
Uncaught TypeError: Cannot read property 'length' of undefined.
当dataTables
获取原始json数据并尝试将其注入表中时,抛出此错误。
// Got the data - add it to the table
for ( i=0 ; i<aData.length ; i++ ) {
_fnAddData( settings, aData[i] );
}
<script>
$(document).ready(function(){
$('#clickMe').click(function(){
$.ajaxPrefilter(function(options, originalOptions, jqHXR ){
options.async = true;
});
$.ajax({
//type: 'GET',
url: 'URL',
dataType:"json",
data: "getAddressResults", //name of json expression
success: function(data){
var jsTable = $('#data').dataTable();
jsTable.fnClearTable();
$.each(data, function(key, value){
var i = this.length;
alert("There are : " + i + " entries."); //shows how many objects are in my json data
jsTable.dataTable().fnAddData([
value[key].childName,
value[key].childNum,
value[key].lineNum,
value[key].parentName,
value[key].parentNum
]);
});
if(data){
var txt = "";
if(res > 0){
for (var i=0;i<data.length;i++){
if (data[i].parentNum && data[i].childNum){
txt += "<tr><td>"+data[i].parentNum+"</td><td>"+data[i].childNum+"</td></tr>"
}
}
if(txt !=""){
$("#data").append(txt).removeClass("hidden");
}
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus + ': ' + errorThrown);
}
});
return false;
});
$('#data').DataTable({
columns:[
{ title: "childName" },
{ title: "childNum" },
{ title: "lineNum" },
{ title: "parentName" },
{ title: "parentNum" }
]
});
答案 0 :(得分:-1)
删除<script>
标记,您不能只在javascript之间放置这样的html标记。