2015-09-06 77 views
0

请帮忙。我有搜索旧的问题,但无法找到相关的解决方案。JQGrid - 网格显示但数据不加载

点击网格显示按钮,但没有数据加载。

我的网站的回复:

{"d":"{"totalpages":2,"currpage":1,"totalrecords":15,"rows":[{"id":"110","cell":["110","perform action 1"]},{"id":"112","cell":["112","perform action 2"]},...]}"} 

我jQuery代码:

$("#b4").click(function() {doAjax4();}); 

    function doAjax4() { 
     $.ajax({ 
      async: false, cache: false, 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "WebAction.aspx/GetDataTable", 
      data: "{}", 
      datatype: "json", 
      success: function (data) { 
       $("#mygrid1").jqGrid({ 
        dataType: "json", 
        type: "POST", 
        colNames: ['runid', 'action'], 
        colModel: [{ name: 'runid', index: 'runid' }, { name: 'action', index: 'action' }], 
        jsonReader: { 
         root: "rows", 
         page: "currpage", 
         total: "totalpages", 
         records: "totalrecords", 
         id: "id", 
         cell: "cell", 
         repeatitems: true 
        }, 
        loadonce: true, 
        viewrecords: true, 
        gridview: true, 
        rowList: [5, 10, 50], 
        caption: "Action Table", 
        height: 'auto', 
        //pager: '#pager', 
        emptyrecords: 'No data for the applied filter' 
       }); 
      }, 
      error: function (xhr, type, exception) { 
       alert(xhr.statusText); 
      } 
     }) 
    } 

回答

0

我看到你的代码中的许多问题:

  • 中的d属性的类型值服务器响应是字符串。所以我想你在Web服务GetDataTable中做了一个典型的初学者错误:你在代码中直接包含了不需要的转换为JSON。正确的将返回对象GetDataTable而不是字符串
  • 您在$.ajax中使用datatype: "json"参数,但应该使用dataType: "json"代替。 jqGrid中close选项的名称是datatype,但$.ajax中的名称是dataType
  • 您使用不需要的参数async: false,我建议您删除它。
  • 你在click处理程序里面调用doAjax4。函数doAjax4通过执行代码$("#mygrid1").jqGrid({ ...})创建success处理程序的内部网格; (“#mygrid1”)。trigger(“reloadGrid”); instead of $(“#mygrid1”)。jqGrid({...});。解决该问题的另一种方法将包括调用GridUnload方法之前$("#mygrid1").jqGrid({ ...});`。如果它存在,它会破坏现有的电网。
  • jqGrid的选项包含dataType: "json"参数而不是datatype: "json",所以jqGrid使用默认选项datatype: "xml",这可能不是你想要的。以同样的方式,您可以使用存在于$.ajax但未在jqGrid中的未知选项type: "POST"。您应该将选项名称从type修复为mtype。如果你想让那个jqGrid发出HTTP POST请求,那么你应该指定url参数(例如url: "WebAction.aspx/GetDataTable"
  • 在第一个Ajax调用中返回的data将被忽略。如果你想要jqGrid使用你以前下载的data,那么你应该使用例如datatype: "jsonstring", datastr: data.d。它将对应您当前的数据格式。
+0

感谢您的快速回复和详细的答案。 **所做的更改 - 它使用jsonstring工作,但我想使用数据类型:“json”&jsonReader。是否有可能或者是方法错**
更正: 1.现在返回GetDataTable对象 2.错字更正为数据类型: “JSON” 3.删除异步:假 4。在成功之前,$(“#mygrid1”)。jqGrid({...});添加$(“#mygrid1”)。trigger('reloadGrid'); \t 5. jqgrid选项(a)错误更正为数据类型:“json”。 (b)错字更正为mtype。 (c)添加url:“WebAction.aspx/GetDataTable” 6.设置数据类型:“jsonstring”,datastr:data.d. –

+0

@BipoK:不客气!您可以删除外部'$ .ajax',并使用带有选项''datatype:“json”,mtype:“POST”,url:“WebAction.aspx/GetDataTable”的jqGrid。你只需要改变'jsonReader'在所有顶层元素中使用'd.'前缀:'jsonReader:{page:“d.currpage”,total:“d.totalpages”,records:“d.totalrecords”,root:“ d.rows“}' – Oleg

+0

再次感谢您。您在这里和其他许多SO中的回复总是有帮助的。您的答案提供了解决方案。我还必须添加2个附加选项。这个用法是否正确?添加的选项是[1]'ajaxGridOptions:{contentType:'application/json; charset = utf-8'}'和[2]'serializeGridData:function(postdata){return null;}' –