[英]How to parse json Data and display it using Ajax in JQuery?
有一个对系统的ajax调用,它返回JSOn作为响应并将其显示在HTML表中。
在HTML中,我们有以下代码将在其中显示数据:
<table id="documentDisplay">
<thead>
<tr>
<th align="left">Document Type</th>
<th align="left">File Name</th>
<th align="left">Document Date</th>
</tr>
</thead>
</table>
以下是Ajax调用和相应的方法调用:
documentCommon.ajax({
blockUI: false,
type: 'POST',
//dataType: 'html',
url : 'loaddocument.json',
data: {
'_CONV_ID': $('input[name="_CONV_ID"]').val(),
},
success: function (data) {
alert(data);
console.log(data);
document.documentDisplay.clear().rows.add(data.lines).draw();
//Here we get an error
},
});
document.documentDisplay= $('#documentDisplay').DataTable($.extend({}, documentCommon.tableTemplate, {
asStripClasses: [], // No stripes
width: "100%",
scrollY: "150px",
columnDefs: [
{"width": "20%", targets: 0},
{"width": "40%", targets: 1},
{"width": "40%", targets: 2},
],
columns: [
{"data": "documentType"},
{"data": "fileName"},
{"data": "documentDate"}
]
}));
Ajax能够发送请求并带回数据。 以下是萤火虫的输出:
[
{
"documentDate":[
2015,
8,
18,
13,
1,
51
],
"documentType":"X",
"documentId":18038,
"fileExtension":"pdf",
"filename":"kuka.pdf",
"link":"http://something.com/kuka.pdf",
},
So on and end with ]
以下是Firebug中显示的错误:
TypeError: a is undefined
代码有问题吗? 还有其他实现相同方法的方法吗? 另外,我想检查数据表中的documentType,即如果documetType为X,则显示否则不显示。 有没有办法做同样的事情?
如果您使用的是jQuery AJAX,则可以通过以下代码直接获取JSON数据: data.responseJSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.