2014-11-06 76 views
1

我從WebService得到Json響應如下Jqgrid沒有綁定數據

Json完美地返回數據。數據看起來像

{「d」:「[{\」ID \「:2,\」Code \「:\」mycode \「,\」Name \「:\」Myname \「,\」PassWord \ 「:\」 A \」,\ 「ClientLevel \」:0,\ 「DEPTNO \」:\ 「\」 \ 「DEPTNAME \」:\ 「\」},{\ 「ID \」:3,\ 「Code \」:\「mycode \」,\「Name \」:\「ly1 \」,\「PassWord \」:\「mypassword \」,.......但不綁定我的jqgrid。

和我有以下的jqGrid代碼

jQuery("#list2").jqGrid({ 
       mtype: 'POST', 
       url: "myservice.asmx/GetQueryInfo",     

       serializeGridData: function (postData) { 
        return JSON.stringify({ 
         TableNames: TableName, 
         ColumnList: ColumnNames 
        }); 
       }, 

       ajaxGridOptions: { contentType: "application/json; charset=utf-8" }, 
       jsonReader: { 
         repeatitems: false, 
         root: 'd',       
         page: function (obj) { return 1; }, 
         total: function (obj) { return 1; }, 
         records: function (obj) { return obj.toString().length; } 
        }, 
       datatype: 'json', 
       colNames: ['ID', 'Code', 'Name', 'PassWord', 'ClientLevel', 'DeptNo', 'DeptName'], 
       colModel: [ 
        { name: "ID", width: 55 }, 
        { name: "Code", width: 90 }, 
        { name: "Name", width: 100 }, 
        { name: "PassWord", width: 80 }, 
        { name: "ClientLevel", width: 80 }, 
        { name: "DeptNo", width: 80 }, 
        { name: "DeptName", width: 150 } 
       ], 
       autoencode: true, 
       gridview: true, 
       rowNum: 10, 
       loadonce: true, 
       rowList: [10, 20, 30], 
       pager: '#pager2', 
       sortname: 'ID', 
       viewrecords: true, 
       sortorder: "ID", 
       caption: "JSON Example", 
       loadError: function (jqXHR, textStatus, errorThrown) { 
        alert('HTTP status code: ' + jqXHR.status + '\n' + 
          'textStatus: ' + textStatus + '\n' + 
          'errorThrown: ' + errorThrown); 
        alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText); 
       } 
      }); 
      jQuery("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false }); 

回答

1

我仍然認爲你在服務器端(WebMethod返回字符串而不是對象),因爲d的值屬性是字符串中使用舊代碼。你可以使用01中的root作爲如在the answer中定義的函數。在這種情況下,它也適用於你的情況。所以你可以使用

jsonReader: { 
    repeatitems: false, 
    root: function (obj) { 
     return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d; 
    } 
}