【问题标题】:Not able to load JSON data in jqGrid无法在 jqGrid 中加载 JSON 数据
【发布时间】:2012-07-18 04:54:10
【问题描述】:

我的jqGrid 定义是

.
.
.
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
                },

.
.
.

我的 JSON 值为

 {"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"]}]}

但我无法将该 JSON 数据加载到 jqGrid 中。这里 jqGrid 显示了网格中的所有记录。

【问题讨论】:

  • 您在尝试什么,结果如何?有什么错误吗?
  • 问题是 jqgrid 没有显示任何记录

标签: javascript jquery ajax json jqgrid


【解决方案1】:

如何从the demo看到你使用的jsonReader确实对应返回服务器的数据。

我想您应该检查服务器响应的 Content-Type 标头。应该是application/json。我建议您另外插入loadError 回调以查看哪个错误 是空网格的原因。详情请见the answer

【讨论】:

  • @BhavikAmbani:对不起,我不明白你的问题。我在the demo 中使用了您在问题中发布的确切 json 数据。所以 JSON 数据本身是正确的。例如,您可以验证 jsonlint.com 中的数据。我想问题出在 HTTP 标头而不是 HTTP 正文中(而不是 JSON 数据本身)。您可以使用Fiddler、Firebug 或 Chrome 或 IE 的开发者工具来捕获 HTTP 流量。您是否尝试另外添加loadError句柄?
  • @BhavikAmbani:在这种情况下,您只需将jquery.jqGrid.min.js 替换为jquery.jqGrid.src.js 并从jquery.jqGrid.src.js 的第1785 行调试$.ajax 请求附近的代码。例如,您应该在 errorsuccess 回调(第 1808 和 1791 行)中设置断点并找出问题的原因。同样,您应该使用jquery.js 而不是jquery.min.js,并且可能在$.ajax 内设置一些额外的断点
  • 我想在服务器端处理更新的值,然后当服务器的肯定响应到来时,我只想更新网格的值。如果服务器响应是否定的,那么我想显示一条错误消息,网格不能更新。
  • @BhavikAmbani:我没有使用 Java。不过,如果您在服务器上处理请求(在 servlet 代码中),您可以验证输入参数。如果参数错误,您必须设置一些错误状态代码 (>=400)。在响应正文中,您可以放置​​自定义错误信息。您可以解码errorTextFormaterrorfuncerrorCell 内部的响应。因为我不使用 Java,也不知道您使用的是哪个基于 Java 的框架,所以我无法更准确地描述如何您可以在您的 servlet 代码中设置错误 HTTP 代码。
  • @BhavikAmbani:您应该使用errorTextFormat 回调,它应该是表单编辑配置的一部分。 Cou 可以在 the answerthis one 中找到示例,您可能会对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
相关资源
最近更新 更多