0

I have a JQGrid plugin in my website, the table is loading OK, but with no-rows to edit, select or whatever. It is requesting the server because when i watch the log I see the request and it's parameters (sidx, _search, rows, sord, nd, etc).

This is the jqgrid code:

$(document).ready(function() {
    var lastsel;
    jQuery("#rowed3").jqGrid(
            {
                url : CONTEXT_PATH+'/ajax/getPartesByCategory.do?catid=<s:property value="categoryId" />',
                datatype: 'json',
                colNames : [ 'piezaId', 'descripcion', 'disponible'],
                colModel : [ {
                    name : 'piezaId',
                    index : 'piezaId',
                    align : "right",
                    width : 40
                }, {
                    name : 'descripcion',
                    index : 'descripcion',
                    width : 360,
                    editable : true
                }, {
                    name : 'disponible',
                    index : 'disponible',
                    width : 80,
                    editable : true
                } ],
                rowNum : 20,
                rowList : [ 20, 40, 60, 80 ],
                pager : '#prowed3',
                sortname : 'id',
                viewrecords : true,
                sortorder : "desc",
                onSelectRow : function(id) {
                    if (id && id !== lastsel) {
                        jQuery('#rowed3').jqGrid('restoreRow', lastsel);
                        jQuery('#rowed3').jqGrid('editRow', id, true);
                        lastsel = id;
                    }
                },
                editurl : "server.php",
                caption : "Piezas"
            });
    jQuery("#rowed3").jqGrid('navGrid', "#prowed3", {
        edit : false,
        add : false,
        del : false
    });
})

This is the JSON that the server returns:

[{
  "piezaId": 486,
  "disponible": 1,
  "descripcion": "Asiento delantero LH",
  "category": {
    "categoryId": 2,
    "category": "Interior",
    "status": 1,
    "subCategories": []
  }
}, {
  "piezaId": 485,
  "disponible": 1,
  "descripcion": "Asiento delantero RH",
  "category": {
    "categoryId": 2,
    "category": "Interior",
    "status": 1,
    "subCategories": []
  }
}]

in the server side I'm using JAVA6, Struts2 and GSon to write the json, but for these ajax calls im only writing to the response a text/plain content

Any help?

Mickael Lherminez
  • 862
  • 1
  • 7
  • 24
Garis M Suero
  • 7,603
  • 7
  • 39
  • 68
  • 1
    Is `CONTEXT_PATH` on the same domain as this page? Also, why aren't you using `application/json` for the responses? – Matthew Flaschen Jul 04 '10 at 00:32
  • Yes, CONTEXT_PATH is in the same domain, it is in another .js that is loaded first (from the template)... Why i'm not using application/json, don't know just started coding and didn't know about application/json. Anyhow i've just realized a test using application/json and the same occurs... – Garis M Suero Jul 04 '10 at 00:44
  • 1
    Shouldn't the JSON be {"piezaId":486, ... } In other words, w/o the square brackets: [ ]. – George Marian Jul 04 '10 at 00:50
  • @George, the JSON is valid. It's an array of two objects. However, having an array as the outermost type does support certain attacks. See [Is it possible to perform a cross site site request forgery attack on a URL that returns a JSON object? ](http://stackoverflow.com/questions/3170196/is-it-possible-to-perform-a-cross-site-site-request-forgery-attack-on-a-url-that). – Matthew Flaschen Jul 04 '10 at 00:54
  • @Matthew OIC that now. Does JQGrid expect a content type of application/json? – George Marian Jul 04 '10 at 01:00
  • after putting total, page, records and rows in the resulted json to look like: `{"total":"1","page":"1","records":"2","rows": [{"piezaId":"486","disponible":"1","descripcion":"Asiento delantero LH"},{"piezaId":"485","disponible":"1","descripcion":"Asiento delantero RH"}]}` im getting the error `Error: u is undefined Source File:http://domain:8084/autoWEB/js/jquery.jqGrid.min.js Line: 53` – Garis M Suero Jul 04 '10 at 01:03

1 Answers1

2

You should either change format of data send from the server or use some jsonReader or jsonmap technique (see for example jquery with ASP.NET MVC - calling ajax enabled web service or http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data and Mapping JSON data in JQGrid). The easiest way is to produce data which can be read with the standard jsonReader (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data):

{
  "total": "1", 
  "page": "1", 
  "records": "2",
  "rows" : [
    {"id" :"1", "cell":["486","1","Asiento delantero LH"]},
    {"id" :"2", "cell":["485","1","Asiento delantero RH"]},
  ]
}
Community
  • 1
  • 1
Oleg
  • 217,934
  • 30
  • 386
  • 757
  • You welcome! Using of `jsonReader` especially with functions you can read practically and JSON data. I like this feature. – Oleg Jul 04 '10 at 01:37