开发者

How to get jqGrid working with Appengine's ProtoRPC

Can anyone please help with what I presume is a simple problem? I'm trying to get Google App Engine to serve JSON data that I can use with jqGrid (v4.0.0), but I seem to have a final fence to jump over. My application is a simple To-Do list example, and using ProtoRPC's form query interface I get output like:

{
rows: [
  {
    status: Planning,
    folder: HPM,
    context: WORK,
    title: PURSUE HPM ACTIONS,
    },
  {
    status: Planning,
    folder: PLANNED MAINTENANCE,
    context: WORK,
    title: PM ASBESTOS,
    },

I try to pick this up with the following jqGrid settings:

      jQuery("#tasklist").jqGrid({
        url:'http://localhost:8084/postservice.get_tasks',
        datatype: 'json',
        mtype: 'GET',
        colNames:['Task','Folder', 'Context','Status'],
        colModel :[ 
          {name:'title', index:'Task', width:120}, 
          {name:'folder', index:'Folder', width:40}, 
          {name:'context', index:'Context', width:32}, 
          {name:'status', index:'Status', width:32} ], 
        pager: jQuery('#pager'),
        rowNum:10,
        jsonReader: {repeatitems:false, cell:'' },
        rowList:[10,20,30],
        sortname: 'folder',
        sortorder: "desc",
        viewrecords: true,
        imgpath: 'themes/basic/images',
        caption: 'My Jobs'
      }); 

However when I run this code, I get one of two responses: if I leave mtype as GET, then jqGrid tries to pass url parameters which are rejected by ProtoRPC:

http://localhost:8084/postservice.get_tasks?_search=false&nd=1304325960695&rows=10&page=1&sidx=folder&sord=desc
400 unrecognized RPC format

If I change mtype to POST then I have a 200 OK response, but no data in my grid, and using 开发者_Python百科Firebug I can't see any evidence that any data was returned.

I have a few things that I've tried, but I have reached the limit of my skills here (not hard!) and I'd appreciate any help.

  • do I need to get ProtoRPC to return the other information that JsonReader expects by default, i.e. total, page, records etc?
  • I tried to follow Oleg's answer , but when I placed the suggested code just in front of the jqGrid settings I had syntax errors and the request never ran:

$.extend($.jgrid.defaults, datatype: 'json', {ajaxGridOptions: { contentType: "application/json" }, {ajaxRowOptions: { contentType: "application/json", type: "PUT" } });

  • I did just try adding contentType: "application/json" to the jqGrid options (ignorance is bliss), but that made no difference
  • Perhaps I am getting the information back, but I'm just not seeing it in Firebug?

Any help will be much appreciated.


PS: Using curl, I am able to return the expected data, so long as I don't try to post any data:

C:\Users\Staples1>curl -i -X POST -HContent-Type:application/json -HAccept:application/json http://localhost:8084/postservice.get_tasks

HTTP/1.0 200 OK Server:

Development/1.0 Date: Mon, 02 May 2011

16:55:19 GMT Cache-Control: no-cache

Content-Type: application/json

Expires: Fri, 01 Jan 1990 00:00:00 GMT

Content-Length: 1069

{"rows": [{"status": "Planning", "folder": "HPM", "context": "WORK", "title": "P URSUE HPM ACTIONS"}, {"status": "Planning", "folder": "PLANNED MAINTENANCE", "co ntext": "WORK", "title": "PM ASBESTOS"}, {"status": "Next Action", "folder": "PL ANNED MAINTENANCE", "context": "WORK", "title": "Chase Brian on asbestos"}, {"st atus": "Undefined", "folder": "PLANNED MAINTENANCE", "context": "WORK", "title": "PM GAS"}, {"status": "Hold", "folder": "PLANNED MAINTENANCE", "context": "WORK ", "title": "Do PBS detail for Gas"}, {"status": "Hold", "folder": "PLANNED MAIN TENANCE", "context": "WORK", "title": "Do next Product Description for Gas Servi cing"}, {"status": "Hold", "folder": "DPA", "context": "WORK", "title": "Put sam ple privacy notices on Rosie"}, {"status": "Hold", "folder": "DPA", "context": " WORK", "title": "Customer Profiling Info"}, {"status": "Hold", "folder": "DPA", "context": "WORK", "title": "Write network access agreement for contractors/ thi rd parties"}, {"status": "Hold", "folder": "DPA", "context": "WORK", "title": "C ontact CMBC for agreement"}]}

C:\Users\Staples1>

And yes, I did buy the laptop from Staples!


PPS: the problem is moving on now.

I finally read Oleg's response again and added to the jqGrid options:

ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },

Now I can see from Firebug that I am eventually sending ContentType:application/json to ProtoRPC, and I'm getting the same error response back that I get when I try to send any data using curl, namely a traceback from AppEngine which finishes

File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson\decoder.py", line 315, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson\decoder.py", line 333, in raw_decode

raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

From Firebug the posted data is

_search=false&nd=1304360758388&rows=10&page=1&sidx=folder&sord=desc

which doesn't seem particularly jsonic. Looks like the only hurdle is how to get jqGrid to send no data, or some properly formatted JSON data, or get ProtoRPC to ignore the data?

Suggestions welcome!


Well it's not pretty, and it won't win any prizes, but I've found that you can turn off the parameters that jqGrid is trying to send to ProtoRPC, by setting the defaults to null in the grid options:

prmNames: {nd:null, search:null, rows:null, page:null, sort:null, order:null},

And now it's populating the grid. Finally I've got to the starting line.

Just to recap, the other bits of the puzzle are

datatype: 'json',
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
mtype: 'POST',

Monkeys and typewriters . . .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜