主要Jsp
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
</table>
</form>
Jquery:
$(document).ready(function() {
$('#example').DataTable( {
"ajax": "json.jsp",
"columns": [
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "extn" },
{ "data": "start_date" },
{ "data": "salary" }
]
} );
} );
json.jsp
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setHeader("pragma", "no-cache");
response.getWriter().write("{data: [ { name: \"Tiger Nixon\", position: \"System Architect\",office: \"Edinburgh\",extn: \"5421\",start_date: \"2011/04/25\",salary: \"$320,800\"}]}");
response.getWriter().flush();
response.getWriter().close();
objects.txt
{ "data": [ { "name": "Tiger Nixon", "position": "System Architect", "salary": "$320,800", "start_date": "2011/04/25", "office": "Edinburgh", "extn": "5421" } ] }
传递json.jsp时的响应:
传递objects.txt:
时的响应
获取无效的json响应。 但是如果我调用objects.txt文件
,相同的代码也可以工作