我正在使用自动填充字段从服务器获取数据并在数据表中显示:
$( “#姓名”)。自动完成({
source: function (request, response) {
$.ajax({
url: "http://myhost.com/webservices/test3.cfm",
data: request,
success: function (data) {
$('#results').dataTable({
"bProcessing": true,
"bJQueryUI": true,
"bLengthChange": true,
"bFilter": true,
"bAutoWidth": false,
"bRetrieve" : true,
"aaData": data ,
"aoColumns": [
{ "sTitle": "Name", "sName": "name" },
{ "sTitle": "Title", "sName": "title" },
{ "sTitle": "Organization", "sName": "organization" },
{ "sTitle": "Email", "sName": "email" },
{ "sTitle": "Status", "sName": "status" }
]
});
}
});
}
从ajax调用返回的数据是:
[[“Steven,Grek”,“President”,“Sands Corp.”,“steven @ yahoo.com”,“1”],[“Steven,Grek”,“Associate”,“Alliance Ltd.”,“steven” @ yahoo.com“,”1“],[”Steven,Grek“,”总统“,”林产品协会“,”steven @ yahoo.com“,”1“]]
我收到以下错误:
如果我更换“aaData”:数据
根据数据的反应:
“aaData”:[[“Steven,Grek”,“President”,“Sands Corp.”,“steven @ yahoo.com”,“1”],[“Steven,Grek”,“Associate”,“Alliance Ltd. “,”steven @ yahoo.com“,”1“,[”Steven,Grek“,”总统“,”林产品协会“,”steven @ yahoo.com“,”1“]
它有效。
知道我做错了吗?
答案 0 :(得分:0)
在同事的帮助下计算出来: 来自我的ajax调用的数据类型是一个字符串。
$("#firstname").autocomplete({
source: function (request, response) {
$.ajax({
url: "http://myhost.com/webservices/test3.cfm",
data: request,
success: function (data) {
var obj = jQuery.parseJSON(data); <---- typeof data is a string
$('#results').dataTable({
"bProcessing": true,
"bJQueryUI": true,
"bLengthChange": true,
"bFilter": true,
"bAutoWidth": false,
"bRetrieve" : true,
"aaData": obj, <---- Use the parsed json object instead
"aoColumns": [
{ "sTitle": "Name", "sName": "name" },
{ "sTitle": "Title", "sName": "title" },
{ "sTitle": "Organization", "sName": "organization" },
{ "sTitle": "Email", "sName": "email" },
{ "sTitle": "Status", "sName": "status" }
]
});
}
});
},
});