从jsp获取json值时获取无效的json错误

时间:2016-11-07 12:08:43

标签: java jquery json ajax jsp

主要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时的响应:

enter image description here

传递objects.txt:

时的响应

enter image description here

获取无效的json响应。 但是如果我调用objects.txt文件

,相同的代码也可以工作

0 个答案:

没有答案