jQWidgets Forums

jQuery UI Widgets Forums Grid JSON.parse error: Unexpected end of data

This topic contains 12 replies, has 3 voices, and was last updated by  Peter Stoev 11 years, 6 months ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
  • JSON.parse error: Unexpected end of data #31470

    thommy
    Member

    I have a jqxGrid but its failing to read data from a json string returned from the server but if i manually create a a json string to simulate the data returned from the server, and pass it to the grid as the datasource its populating correctly. Here is the format of the data:

    Manually created json string variable:
    var datasource ='[{ “Col1”: “value1”, “Col2”: “Value2”},{“Col1”: “Value3″,”Col2″: Value3”}]’;

    The returned json string from the server:

    [{ “Col1”: “value1”, “Col2”: “Value2”},{“Col1”: “Value3″,”Col2″: Value3”}]

    If I check in the debbugger of the browser, the error is: Syntax error: json.parse: unexpected end of data. How can I fix this problem? What am I doing wrongly?

    I count on your usual assistance.

    JSON.parse error: Unexpected end of data #31483

    Peter Stoev
    Keymaster

    Hi thommy,

    As far as I see, the second JSON has a syntax error indeed – “Col2″: Value3”. It should be: “Col2”: “Value3”

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    JSON.parse error: Unexpected end of data #31486

    thommy
    Member

    Thanks Peter for the reply.

    Let me paste the actual data in my code for the comparison.

    Manually created json string variable:

    var datasource ='[{ “DeptName”: “Finance”, “DeptID”: “Fin001”},{“DeptName”: “Human Resources”,”DeptID”: “HR003”},{ “DeptName”: “Infomation Technology”, “DeptID”: “IT002”},{ “DeptName”: “Operations”, “DeptID”: “Opts004”},{ “DeptName”: “Treasury”, “DeptID”: “Trsy-03”}]’;

    Returned JSON string from the server:

    [{“DeptName”: “Finance” ,”DeptID”: “Fin001”},{“DeptName”: “Human Resources” ,”DeptID”: “HR003”},{“DeptName”: “Infomation Technology” ,”DeptID”: “IT002”},{“DeptName”: “Operations” ,”DeptID”: “Opts004”},{“DeptName”: “Treasury” ,”DeptID”: “Trsy-03”}]

    I cant seem to find the error.

    JSON.parse error: Unexpected end of data #31487

    Peter Stoev
    Keymaster

    Hi thommy,

    Sorry, but I do not find an error and that data is loaded correctly in the Grid.

    Example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title id='Description'>This example shows how to create a Grid from JSON data.</title>
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.columnsresize.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var url = "jsondata.txt";
    // prepare the data
    var source =
    {
    datatype: "json",
    datafields: [
    { name: 'DeptName', type: 'string' },
    { name: 'DeptID', type: 'string' }
    ],
    url: url
    };
    var dataAdapter = new $.jqx.dataAdapter(source);
    $("#jqxgrid").jqxGrid(
    {
    source: dataAdapter,
    columns: [
    { text: 'DeptName', datafield: 'DeptName', width: 250 },
    { text: 'DeptID', datafield: 'DeptID' }
    ]
    });
    });
    </script>
    </head>
    <body class='default'>
    <div id="jqxgrid"></div>
    </body>
    </html>

    The jsondata.txt

    [{"DeptName": "Finance" ,"DeptID": "Fin001"},{"DeptName": "Human Resources" ,"DeptID": "HR003"},{"DeptName": "Infomation Technology" ,"DeptID": "IT002"},{"DeptName": "Operations" ,"DeptID": "Opts004"},{"DeptName": "Treasury" ,"DeptID": "Trsy-03"}]

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    JSON.parse error: Unexpected end of data #31489

    thommy
    Member

    Peter,

    I am able to populate the grid without errors if I use the datasource I created manually, but if I use the returned json string from the server directly, the browser is throwing the json.parse unexpected end of data error. Am not sure what I am doing wrongly.

    JSON.parse error: Unexpected end of data #31502

    Peter Stoev
    Keymaster

    Hi thommy,

    Isn’t that the “[{“DeptName”: “Finance” ,”DeptID”: “Fin001”},{“DeptName”: “Human Resources” ,”DeptID”: “HR003”},{“DeptName”: “Infomation Technology” ,”DeptID”: “IT002”},{“DeptName”: “Operations” ,”DeptID”: “Opts004”},{“DeptName”: “Treasury” ,”DeptID”: “Trsy-03″}]” data that your server returns? Because that data was displayed on the sample I posted. I suggest you to check whether the data that your server returns is formatted correctly. If it is not, then you have 2 options – fix what your server returns or format your data before trying to parsing it to JSON on the client side.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    JSON.parse error: Unexpected end of data #31538

    thommy
    Member

    It is the exact data returned from the server. If I parse it directly to the jqxGrid it’s saying no data to display. And when I try to format it ,still the jqxgrid widget is failing to display the data. How do I correctly format it and parse it to JSON?

    JSON.parse error: Unexpected end of data #31547

    bozpet
    Member

    Use JSON.stringify(“your data from server here”).

    JSON.parse error: Unexpected end of data #31550

    Peter Stoev
    Keymaster

    Hi thommy,

    Have you tried the sample I posted? The Data comes from URL and especially from a file I named jsondata.txt.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    JSON.parse error: Unexpected end of data #31554

    thommy
    Member

    Hi Peter,

    I have tried it’s working but when I try to retrieve the data from the server of my application, it’s giving no errors. If I use the function JSON.stringify(sample data), the grid is not populating still even when the json string is correctly formatted.

    JSON.parse error: Unexpected end of data #31562

    Peter Stoev
    Keymaster

    Hi thommy,

    Is the server on the same domain as your application? If it is not, you would probably need to use “jsonp” as a data type instead of “json”. Sorry, but I do not understand what is the problem, because the sample I sent you simulates client-server binding where the server is on the same domain and returns the data string that you sent me. For debugging, I would suggest you to use the jqxDataAdapter’s loadComplete and loadError callbacks. If there’s some error in the connection, the loadError would be called with 3 helpful params – jqXHR, status, error.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    JSON.parse error: Unexpected end of data #31655

    thommy
    Member

    Hi Peter,

    Thanks for your untiring support I however managed to fix the problem. It wasn’t related to JSON format but for some reason ASP.NET MVC was not loading the returned JSON data from the server fo a reason I dont know. So instead of the writing the javascript code that fetches data from a controller separately in another file and attach it to the view using tags, I just added the code to the cshtml page just after the header between …here… tags. And boom! it worked!!

    Thanks!

    JSON.parse error: Unexpected end of data #31666

    Peter Stoev
    Keymaster

    Hi thommy,

    Thanks for the update.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.