0

My jqGrid definition is

.
.
.
datatype: 'json', //We specify that the datatype we will be using 
url:'<%=request.getContextPath()%>/servlet/AjaxManager?mode=9999&beginindex=0&totallimit=10&colname=policyname&sorttype=asc', // that will return the data
colNames:['Policy Name','Policy Type', 'Time allowed (HH:mm)','Expiration Duration (days)','Session Pulse(minutes)','Description'],  //Column Names
colModel :[ 
    {name:'policyname', index:'policyname', editable:true,sorttype:'text',width:150,editoptions:{size:30,maxlength:50}, frozen : true,editrules:{required:true},formatter:'showlink',formatoptions:{baseLinkUrl:'javascript:' , showAction: "GetAndShowUserData(jQuery('#list2'),'",addParam: "');"}},
    {name:'policytype', index:'policytype', sorttype:'text',editable:true,edittype:"select",editrules: {edithidden:true},editoptions:{value:"POST:Postpaid;PRE:Prepaid"}},
    {name:'allotedminutes', index:'allotedminutes',resizable:false, sorttype:'text',editable:true,width:200, align:"right",editoptions:{size:10}},
    {name:'expiredays', index:'expiredays',  sorttype:'text',editable:true,width:200, align:"right",editrules:{integer: true},editoptions:{size:5, maxlength:4}},       
    {name:'sessionpulse', index:'sessionpulse',sorttype:'int',editable:true,width:200, align:"right"},      
    {name:'policydescription', index:'policydescription', sortable:false,editable:true,sorttype:"date"}],
.
.
.
jsonReader : {
                    root: "ROWS",               //our data
                    page: "CURRENTPAGE",        //current page
                    total: "TOTAL",             //total pages
                    repeatitems: true,
                    id: "id",
                    cell: "cell",
                    userdata:"USERDATA", //Userdata we will pass back for feedback
                    records: "TOTALRECORDS"     //total records
                },

.
.
.

And my JSON value is

 {"TOTAL":1,"CURRENTPAGE":1,"TOTALRECORDS":1,"ROWS":[{"id":1,"cell":["Unlimited Policy","Absolute","Unlimited","Unlimited",1,"2007-12-03"]},{"id":2,"cell":["1 Month Unlimited policy","Absolute","Unlimited",30,1,"2007-12-03"]},{"id":3,"cell":["100 Hours policy","Absolute","100:00","Unlimited",1,"2007-12-03"]}]}

But I am not able to load that JSON data into jqGrid. Here jqGrid is showing any records in the grid.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86

1 Answers1

1

How you can see from the demo the jsonReader which you use do corresponds to the data which returns the server.

I suppose that you should examine the Content-Type header of the server response. It should be application/json. I recommend you additionally to insert loadError callback to see which error was the reason of the empty grid. See the answer for more details.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @BhavikAmbani: Sorry, but I don't understand your question. I used in [the demo](http://www.ok-soft-gmbh.com/jqGrid/BhavikAmbani.htm) the exact json data which you posted in your question. So the JSON data itself are correct. You can validate the data in http://jsonlint.com/ for example. I suppose that the problem is in the HTTP header and not in the HTTP body (not in JSON data itself). You can use [Fiddler](http://www.fiddler2.com/fiddler2/), Firebug or Developer Tools of Chrome or IE to catch the HTTP traffic. Do you tried to add `loadError` handle additionally? – Oleg Jul 18 '12 at 05:44
  • when I placed error function 'loadError' it gave me parsererror. – Bhavik Ambani Jul 18 '12 at 06:03
  • @BhavikAmbani: "parsererror" are typical error if jQuery.ajax parse the server response corresponds to the `Content-Type` which not corresponds to the data. For example if you would have `text/xml` or `application/xml` for example in the `Content-Type` the server response will try to parse the JSON data returned from the server as XML document and you will have the error "parsererror". You should use Fiddler, Firebug or "Network" tab of the Developer Tools of Chrome or IE to examine the HTTP headers of the server response. I suppose that you has wrong `Content-Type` (html, xml etc). – Oleg Jul 18 '12 at 06:34
  • I have used content type `application/json`. – Bhavik Ambani Jul 18 '12 at 06:36
  • 1
    @BhavikAmbani: In the case you should just replace `jquery.jqGrid.min.js` to `jquery.jqGrid.src.js` and debug the code near the `$.ajax` request from the line 1785 of `jquery.jqGrid.src.js`. For example you should set breakepoints in `error` and `success` callbacks (lines 1808 and 1791) and find out the reason of your problem. In the same way you should use `jquery.js` instead of `jquery.min.js` and probably set some additional breakpoints inside of `$.ajax` – Oleg Jul 18 '12 at 07:56
  • @BhavikAmbani: What was the reason of the problem? How you fixed the problem? – Oleg Jul 18 '12 at 09:46
  • the problem was that I wan in the JQgrid configuration. – Bhavik Ambani Jul 18 '12 at 09:50
  • I have one more problem if you can solve. I want to edit rows of the grid and want to wait until server response comes. If the response of the server is success then only I want to update the grid else give an error in the grid. Thanks in advance. – Bhavik Ambani Jul 18 '12 at 13:04
  • @BhavikAmbani: What is your current problem? Do you implemented the server side changes for the row editing and you have problem with the error reporting? Which editing mode you use ([inline editing](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing) of [form editing](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing))? – Oleg Jul 18 '12 at 13:34
  • 1
    I want to process the updated value server side and then when server's positive response comes then only I want to update the value of the grid. If the server respondes negative then I want to display an error message, grid must not be updated. – Bhavik Ambani Jul 19 '12 at 03:55
  • @BhavikAmbani: It is the default behavior in jqGrid. Which editing mode you use? Do you already implemented the server side changes for the row editing or you don't know how to implement it? Do you have problem with the error reporting? – Oleg Jul 19 '12 at 04:10
  • I get server side request but I want to know that if there comes any error in server side processing and if I want to throw and error then how can I do that ? And I also dont want to update the values at the client side in JQGrid. – Bhavik Ambani Jul 19 '12 at 04:25
  • @BhavikAmbani: You need to set some error HTTP status code in the response of the server if you need to report an error. In the case the grid will be not updated and you can specify your alternative callback which get the server response and which display the error message. The error callback **depend on the editing mode which you use**. For example it's `errorTextFormat` for the form editing, `errorfunc` for inline editing and `errorCell` for cell editing. It's the reason why I asked you many times which editing mode you use. – Oleg Jul 19 '12 at 04:39
  • @BhavikAmbani: The server code depends not only from the language which you use, but from the technology which you chosen. For example in ASP.NET it could be `context.Response.StatusCode = (int)HttpStatusCode.BadRequest;` or `throw new FaultException("Error message");` or just any `throw new ...`. I am not use Java, so can't provide you any Java code. Probably the code can be something like `ServletActionContext.getResponse().setStatus(400)`. Probably like `return Response.status(Response.Status.BAD_REQUEST).entity("My Error text")` – Oleg Jul 19 '12 at 05:18
  • but lets say if I am making any update request and if there is any problem with data regarding the updation of the records then how can I do that ? I am also using `Java` at the server side hence you can help me better way. – Bhavik Ambani Jul 19 '12 at 05:42
  • Please provide me samples codes for `CRUD` operations with `Java` as server side as soon as possible. – Bhavik Ambani Jul 19 '12 at 06:32
  • 1
    @BhavikAmbani: I am not using Java. Nevertheless if you process the request on the server (in the servlet code) you can validate the input parameters. In case of wrong parameters you have to set some error status code (>=400). In the body of the response you can place your custom error information. You can decode the response inside of `errorTextFormat`, `errorfunc` or `errorCell`. Because I don't use Java and don't know which Java based framework you use I can't describe more exactly *how* you can set error HTTP code in your servlet's code. – Oleg Jul 19 '12 at 06:35
  • @BhavikAmbani: I **don't use Java** myself. So I can't provide you any code example in Java. Moreover the implementation will be *absolutely different* depend on the framework which you use. For example the String 3 code will help you not if you use it not. – Oleg Jul 19 '12 at 06:37
  • Will you please provide the sample code whatever languge you prefer. I will try to understand, but the main thing is in that there should be `CRUD` operations. – Bhavik Ambani Jul 19 '12 at 07:13
  • @BhavikAmbani: 1) in the code of `ProcessRequest` from [the answer](http://stackoverflow.com/a/10871428/315935) you can see how `context.Response.StatusCode` will be explicitly set. In the same way you can set `(int)HttpStatusCode.BadRequest` value as the `StatusCode`. 2) In [the answer](http://stackoverflow.com/a/5501644/315935) you can see `HandleJsonExceptionAttribute` class which will be used as `[HandleJsonException]` attribute. It serialize *any* exception as JSON response. – Oleg Jul 19 '12 at 07:57
  • @BhavikAmbani: [Here](http://blogs.msdn.com/b/endpoint/archive/2010/01/21/error-handling-in-wcf-webhttp-services-with-webfaultexception.aspx) you find examples how to use `WebFaultException` to report errors from WFC-based code of you server implementation. You can see that all the implementations are **very different**. So you should just examine *which ways there are in the technology which you use* to set the status code of the HTTP response. – Oleg Jul 19 '12 at 07:59
  • I want to process the server response which I sent as error message and should be displayed on the add form. Currently it displayes `error Status: 'error'. Error code: 756`. But I also want to process the message which was sent from server. – Bhavik Ambani Jul 19 '12 at 10:13
  • 1
    @BhavikAmbani: You should use `errorTextFormat` callback which should be the part of the form editing configuration. Cou could find examples in [the answer](http://stackoverflow.com/a/6803206/315935) and in [this one](http://stackoverflow.com/a/5103844/315935) which you can by probably helpful for you. – Oleg Jul 19 '12 at 15:03