请参考下面的代码,当使用innerData时,它是工作的,但当使用outerData时,它不工作,并告诉我:
请求行0的未知参数“徽章”
我的innerData和outerData看起来是一样的,只是值不同。
如果有人能给我指明正确的方向,我会很感激的。
EDIT:Gyrocode.com在下面的评论中建议JSON.parse(outerData),它的功能就像一种魅力。
$("#ButtonSearch").click(function(){
try {
companyCode = '<%=Session["comp_code"].ToString()%>';
$.ajax({
type: "POST",
url: "MtForm.aspx/helloWorld",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: "{companyCode:" + JSON.stringify(companyCode) + "}",
success: function(outerData){
var innerData = [{"badge":"11111111","name":"John Doe","ext":"2222","dept_name":"3333"},{"badge":"44444444","name":"Jane Doe","ext":"5555","dept_name":"6666"}];
$('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"><thead><tr><th>Badge</th><th>Name</th><th>Ext.</th><th>Dept.</th></tr></thead></table>' );
$('#example').dataTable({
"data": innerData,
"columns": [
{ "data": 'badge' },
{ "data": 'name' },
{ "data": 'ext' },
{ "data": 'dept_name' }
]
});
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
console.log(status);
console.log(error);
alert("error");
}
});
}
catch(err) {
alert('Javascript error: ' + err.message);
}
});发布于 2015-08-04 10:30:58
DataTables能够从Ajax可以获得的几乎任何JSON数据源读取数据。这可以通过设置ajax选项,以其最简单的形式来完成。见文档
在你的例子中,应该是这样的:
var table = $('#example').dataTable({
"ajax": {
"url": "MtForm.aspx/helloWorld",
"data": function (d) {
return JSON.stringify(
$.extend( {}, d, {
"companyCode ": '<%=Session["comp_code"].ToString()%>'
})
)
},
type: "POST",
contentType: "application/json",
},
deferLoading: true,
"columns": [
{ "data": 'badge' },
{ "data": 'name' },
{ "data": 'ext' },
{ "data": 'dept_name' }
]
});
// execute request when all necessary data were loaded
table.draw();https://stackoverflow.com/questions/31806593
复制相似问题