2011-03-14 81 views
1

是否有任何方法可以使我可以从Web服务返回的DataSet中填充数据列表。我想使用$ .ajax jquery函数。 如果是的话,请给我一个小例子。

+4

请参阅以下文档:http://api.jquery.com/jQuery.ajax/ – 2011-03-14 17:35:14

+0

除上述文档外,您是否有具体问题或功能对您有所挑战? – 2011-03-14 18:29:27

回答

0

不,你不能这样做。您必须使用简单的类型创建您的自定义类并将其返回。

3

这个问题有点老...但我会回答。

我会推荐使用自定义类,但它可能使用DataSets。

jQuery代码:

<script type="text/javascript"> 
    $.ajax({ 
     type: "POST", 
     url: "Default.aspx/GetSomeData", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "xml", 
     success: function (msg) { 
     $(msg).find('Table').each(function (i, row) { 
      alert($(row).find('Field').text()); 
     }); 
     } 
    }); 
    </script> 

C#代码:

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)] 
    public static string GetSomeData() 
    { 
     var dataSet = new DataSet(); 

     // Use proper try-catch! 
     string connStr = "Connection String Here"; 
     using (var conn = new SqlConnection(connStr)) 
     { 
      using (var com = new SqlCommand("select top 5 ID, Field from Table", conn)) 
      { 
       var adp = new SqlDataAdapter(com); 
       adp.Fill(dataSet); 
      } 
     } 

     return dataSet.GetXml(); 
    } 

注:我用DataSet.GetXml方法,因为生成的XML更简单,因为你可能会得到一些weird errors刚刚回国DataSet是一样的。