2012-02-09 108 views
1

我在.net應用程序中使用JQGrid。我使用在線代碼示例,它不會拋出任何錯誤,但它不起作用,要麼不知道我做錯了什麼。有些人在使用JQgrid之前可以查看我的代碼,並向我展示iam做錯了什麼。我能夠進入服務器端代碼,它不會拋出任何錯誤。真的不知道我做錯了什麼。JQGrid與web服務不加載數據

這裏是aspx頁面:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title></title> 
<link rel="stylesheet" type="text/css" media="screen" href="jquery-ui-1.8.17.custom.css" /> 
<link rel="stylesheet" type="text/css" media="screen" href="ui.jqgrid.css" /> 

    <script src="jquery.js" type="text/javascript"></script> 

    <script src="grid.locale-en.js" type="text/javascript"></script> 

    <script src="jquery.dataTables.min.js" type="text/javascript"></script> 

    <script src="jquery.jqGrid.min.js" type="text/javascript"></script> 

調用gquery負載電網

<script type="text/javascript"> 
     jQuery(document).ready(function() { 
      jQuery("#list").jqGrid({ 
       type: "GET", 
       url: "MyService.asmx/GetRecipie", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 

       colNames: ['Inv No', 'Date', 'Amount'], 
       colModel: [ 
     { name: 'job_id', index: 'job_id', width: 55 }, 
     { name: 'job_num', index: 'job_num', width: 90 }, 
     { name: 'order_num', index: 'order_num', width: 80, align: 'right'}], 
       pager: jQuery('#pager'), 
       rowNum: 10, 
       rowList: [10, 20, 30], 
       sortname: 'id', 
       sortorder: "desc", 
       viewrecords: true, 
       imgpath: 'themes/basic/images', 
       caption: 'My first grid', 
       loadError: Error 

      }); 
     }); 

     function Error(xhr, st, err) { 
      jQuery("#rsperror").html("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText); 
     } 


    </script> 

</head> 
<body> 
    <table id="list" class="scroll"> 
    </table> 
    <div id="pager" class="scroll" style="text-align: center;"> 
    </div> 
</body> 
</html> 

我的web

 [WebMethod] 
     [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)] 
     public string GetRecipie() 
     { 
      string strQuery = "SELECT * FROM job where job_id like '%2345%'"; 
      DataTable dtRecipie = null; 
      Recipie objRecipie = default(Recipie); 
      SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;DATABASE=TESTDB;Data Source=SQL;"); 
      using (con) 
      { 
       con.Open(); 
       using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strQuery, con)) 
       { 
        dtRecipie = new DataTable(); 
        sqlAdapter.Fill(dtRecipie); 
       } 
      } 
      List<Recipie> drlist = new List<Recipie>(); 
      foreach (DataRow dr in dtRecipie.Rows) 
      { 
       objRecipie = new Recipie(); 
       objRecipie.jobId = Convert.ToInt32(dr["job_id"].ToString()); 
       objRecipie.JobNumber = dr["job_num"].ToString(); 
       objRecipie.OrderNumber = dr["order_num"].ToString(); 
       drlist.Add(objRecipie); 
      } 

      JavaScriptSerializer jSearializer = new JavaScriptSerializer(); 
      return jSearializer.Serialize(drlist); 

     } 
    } 


    public class Recipie 
    { 
     public int jobId; 
     public string JobNumber; 
     public string OrderNumber; 
    } 


} 
+0

是因爲您有UseHttpGet = false,但您的Ajax調用是使用GET而不是POST進行的? – Tuan 2012-02-09 20:16:11

+0

@Tuan:一般來說你是對的,但是HTTP GET不會因爲'type:「GET」'而被使用,而是因爲參數的正確名稱應該是'mtype:「POST」'。參數'dataType:「json」'也是錯誤的(錯誤的情況)。所以默認'datatype:「xml」'將被jqGrid使用。發佈的代碼包含很多錯誤,因此列表太長。例如,不要從web方法返回'drlist'對象('List '),而是手動調用'JavaScriptSerializer.Serialize'。結果字符串將再次被JS​​ON編碼。我可以繼續... – Oleg 2012-02-09 20:48:30

+0

@ user1098028:我建議你閱讀[答案](http://stackoverflow.com/a/3161542/315935)。 [這裏](http://stackoverflow.com/a/4031603/315935)和[這裏]你可以找到一些演示項目的鏈接,你可以下載。我希望你可以修改演示到你的環境。 – Oleg 2012-02-09 20:52:38

回答

0

1 - 你的序列化的實體名單無將它們更改爲jqgrid能夠理解的格式。

您應該返回一個JSON看起來像這樣:

{ 
    "page":"1", 
    "total":4, 
    "records":"10", 

    "rows":[ 
     {"id":"1","cell":["Prabir","Shrestha"]}, 
     {"id":"2","cell":["Bill","Gates"]}, 
     {"id":"3","cell":["Steve","Ballmer"]} 
    ] 
} 

來源:http://blog.prabir.me/post/Using-jqGrid-with-ASPNET-Web-Forms-e28093-Part-I.aspx。本網站介紹瞭如何將數據轉換爲此格式。

還有其他的東西,在你使用的webservice中UseHttpGet = false,而你正在初始化jqgrid type: "GET"。你需要改變其中之一。