ThEBiShOp
ThEBiShOp

Reputation: 516

jqgrid: can't display json data

I'm trying to display json data in a jqgrid and I don't get any row.

Here is my grid :

$('#jqList').jqGrid({
        url: '/my/url',
    datatype: 'json',
    colNames:['field1', 'field2'],
    colModel:[
        {name:'field1',index:'field1', width:200},
        {name:'field2',index:'field2', width:300}
    ],
    pager: '#jqPager',
        rowNum: 20,
        rowList: [10, 20, 30, 50],
        height: '100%',
        viewrecords: true,
        jsonReader: {
            repeatitems: false,
            root: function (obj) { return obj; },
            page: function (obj) { return 1; },
            total: function (obj) { return 1; },
            records: function (obj) { return obj.length; },
            id: 'id'
        }
});

I can access my url in my browser and it displays something like this :

    {"rows": [
        {"id": "3", "field1": "blabla", "field2": "blabla"},
        {"id": "10", "field1": "blabla", "field2": "blabla"},
        {"id": "4", "field1": "blabla", "field2": "blabla"},
        {"id": "8", "field1": "blabla", "field2": "blabla"}
    ]
}

But when I display my grid, I don't have any data as I said. If I check the console, I have a 500 error on my url (with some params added to the url)

I hope I gave enough informations.

edit firebug console says :

"NetworkError: 500 Internal Server Error - http://localhost/path/to/my/url?_search=false&nd=1373439192511&rows=20&page=1&sidx=&sord=asc"

I'm using PHP on apache server

php code :

header('Content-type: application/json');
foreach ($cars as $k => $v) {
    $tab['rows'][$k] = $v['Car'];
}
echo json_encode($tab);

Thanks

Upvotes: 0

Views: 1221

Answers (3)

JEuvin
JEuvin

Reputation: 1037

Usually when I get 500 errors it means that the url path is wrong for javascripts or mvc controllers.

First thing is first,

Your datamap, is it in the same file as your grid? If it is then give it a name and reference that name in the url. If you are getting the data from a db, then you probably using a controller to get the data back and converting it to a json serializable object. So the url should point to the location of that controller.

Upvotes: 0

ThEBiShOp
ThEBiShOp

Reputation: 516

It seems that jqGrid was waiting a url like that : path/to/my/json/url despite I gave url : path/to/my/url on the development server... and url : path/to/my/url works fine on the beta server...

I don't really understand why, if the url was "wrong" why the error wasn't 404 ?

Anyway it's working fine now...

Thanks for your time

Upvotes: 0

tpeczek
tpeczek

Reputation: 24125

It seems that the URL generated by jqGrid:

http://localhost/path/to/my/url?_search=false&nd=1373439192511&rows=20&page=1&sidx=&sord=asc

Is causing some kind of error on the server side (I don't know which server side technology are you using but for example in case of ASP.NET this kind of response means that there was unhandled exception).

You have stated that the clean URL, which should look like this:

http://localhost/path/to/my/url

Is working properly. In that case you, if you don't need the parameters which jqGrid is adding you can try to work around the problem by telling jqGrid not to send them:

$('#jqList').jqGrid({
    url: '/my/url',
    datatype: 'json',
    colNames:['field1', 'field2'],
    colModel:[
        {name:'field1',index:'field1', width:200},
        {name:'field2',index:'field2', width:300}
    ],
    pager: '#jqPager',
    rowNum: 20,
    rowList: [10, 20, 30, 50],
    height: '100%',
    viewrecords: true,
    prmNames: {
        page: null,
        rows: null,
        sort: null,
        order: null,
        search: null,
        nd: null
    },
    jsonReader: {
        repeatitems: false,
        root: function (obj) { return obj; },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; },
        id: 'id'
    }
});

This is not a perfect solution (we still doesn't know why your server is generating internal error and how to solve that) but it might make your code work. If you want to address the server side problem we must know more about what is happening on the server.

Upvotes: 1

Related Questions