3

I follow the steps of this example(http://www.jqwidgets.com/jquery-widgets-documentation/documentation/java-integration/bind-jquery-grid-to-mysql-database-using-jsp.htm), but there’s no data to display.

jqxgrid.jsp file:

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();

In jsp file I can get the result of JsonArray:

[{"id":"57","name":"aa"},{"id":"58","name":"qq"},{"id":"59","name":"ii"},{"id":"60","name":"jenny"},{"id":"61","name":"candy"},{"id":"62","name":"f"},{"id":"63","name":"pp"},{"id":"66","name":"kkk"}]

jqxgrid.html file:

 $(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
            }]
        });
    });

There's grid in output but shows no data to display. (Sorry, I can't post an image.)

Got error:

Status =parsererror, Error =SyntaxError: JSON Parse error: Unrecognized token '<'

How can I fix this problem? Thanks!

Community
  • 1
  • 1
Rebecca_chu
  • 71
  • 1
  • 7

1 Answers1

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.

Blindman67
  • 51,134
  • 11
  • 73
  • 136
  • Thank you so much! I have add type to my datafields, but it's still not working. Actually I've tried lots kinds of data including jqwidget's sample database. None of those worked. Only when I use local data or remote data(like this one: http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxgrid/index.htm#demos/jqxgrid/bindingtojsonp.htm). So I think this problem probably comes from json format. – Rebecca_chu May 25 '15 at 09:53
  • If the JSON example you provided is the one you are using there is no problem with the file. In the console you can type `JSON.parse(jsonString)` with `jsonString` being the JSON file content. Do it on a empty page and then on a jpxgrid page. They may be using a polyfill for `JSON.parse` or may have their own json parser. That is where I suspect the problem is as there is nothing wrong with your JSON string. – Blindman67 Jun 01 '15 at 06:06
  • If that does not help, try checking the MIME type for JSON jsp file. Officially it should be `application/json` but you can also use the unofficial `text/json' or 'text/javascript' for jsp MIME types. – Blindman67 Jun 01 '15 at 06:12