JQWidgets - Jqxgrid"没有要显示的数据",JSON Parse错误

时间:2015-05-23 16:42:25

标签: javascript json jsp jqxgrid jqwidget

我按照此示例的步骤(http://www.jqwidgets.com/jquery-widgets-documentation/documentation/java-integration/bind-jquery-grid-to-mysql-database-using-jsp.htm),但没有数据要显示。

jqxgrid.jsp文件:

ResultSet result = state.executeQuery(sql1);
JsonArray recordsArray = new JsonArray();
while (result.next()) {
    JsonObject currentRecord = new JsonObject();
    currentRecord.add("id",
            new JsonPrimitive(result.getString("id")));
    currentRecord.add("name",
            new JsonPrimitive(result.getString("name")));
    recordsArray.add(currentRecord);
}

out.print(recordsArray);
out.flush();

在jsp文件中,我可以得到JsonArray的结果:

  

[{" ID":" 57""名称":" AA"},{" ID&#34 ;:" 58""名称":" QQ"},{" ID":" 59",& #34;名称":"ⅱ"},{" ID":" 60""名称":"珍妮"},{" ID":" 61""名称":"糖果"},{" ID& #34;:" 62""名称":" F"},{" ID":" 63" "名称":" PP"},{" ID":" 66""名称":&# 34; KKK"}]

jqxgrid.html文件:

 $(document).ready(function () {

        var source = {
            datatype: "json",
            datafields: [{name: 'id'}, 
                         {name: 'name'}],
           url:"jqxgrid.jsp"
        };

        var dataAdapter = new $.jqx.dataAdapter(source, {
            downloadComplete: function (data, status, xhr) { },
            loadComplete: function (data) { },
            loadError: function (xhr, status, error) {alert('Status ='+ status +',  Error ='+ error ); }
        });
        $("#jqxgrid").jqxGrid({
            width: 400,
            autoheight: true,
            source: dataAdapter,
            columns: [{
                text: 'ID',
                datafield: 'id',
                width: 200
            }, {
                text: 'Name',
                datafield: 'name',
                width: 200
            }]
        });
    });

输出中有网格,但没有显示数据。 (对不起,我无法发布图片。)

收到错误:

  

Status = parsererror,Error = SyntaxError:JSON解析错误:无法识别的令牌'<'

如何解决此问题?谢谢!

1 个答案:

答案 0 :(得分:0)

I think your problem is that you are not typing the source data description (client side). The id of the 4th record is 60 which happens to be the < character. The documentation said that type is a required field (though their examples don't always use it.)

If you add type datafields:[{name:'id',type:'int'}, to the var source = { this may solve your problem. You could also use 'number' as the type for id. It may pay as well to add the type 'string' to the name field.

Hope this works for you.