通过JQuery从Database到datatable到generic collection到JSon

时间:2012-06-28 10:30:52

标签: jquery asp.net json service web

好人,我处于两难境地,时间绝对不在我身边。

我的目标 我希望通过数据集/数据表从数据库获取记录,然后通过客户端使用JQuery获取通用集合,最后到JSon对象,以改善用户体验。

到目前为止有效 我的代码在Web服务中工作,直到数据被移交给客户端/浏览器。

我不确定 由于我很少或几乎没有使用过JSon和JQuery的经验,因此Web服务中的数据不会显示在Web浏览器中。我不知道问题出在哪里。

代码 在这里,我附上我的其余代码:

  1. hotelrates.vb文件,hotelrates.asmx文件背后的代码
  2. Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.Web.Script.Services
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Data
    Imports MySql.Data.MySqlClient
    Imports System.Reflection
    
    <WebService([Namespace]:="http://tourslogin.net/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ScriptService()> _
    Public Class Hotelrates
     Inherits WebService
     <WebMethod()> _
     Public Function GetRates() As Generic.List(Of Rate)
        Dim m_globalfunctions As New clsGlobalFunctions
        Dim SQL As String = "SELECT 
    
         

    tblhotelrates.ID,Supplier,From_date,To_Date,Room_type,Room_basis FROM   tblhotelrates,tblHotels WHERE tblhotelrates.SupplierID = tblHotels.ID“

         Dim clsDB As New clsDatabase("jksafarisdbConnectionString")
         Dim dsCostingDetails As DataSet
         Try
            dsCostingDetails = clsDB.QueryDataSet(SQL, "jksafarisdbConnectionString")
    
            Dim s As Rate
            Dim c As New Generic.List(Of Rate)
            For Each dr As DataRow In dsCostingDetails.Tables(0).Rows
                s = New Rate
                With s
                    .myID = m_globalfunctions.CheckDBNull(dr("ID"))
                    .Supplier = m_globalfunctions.CheckDBNull(dr("Supplier"))
                End With
                c.Add(s)
            Next
    
            Return c
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            clsDB.Close()
        End Try
    
    End Function End Class <Serializable()> _ Public Class Rate
    Private _myID As String
    Private _supplier As String
    
    Public Property myID() As String
        Get
            Return _myID
        End Get
        Set(ByVal value As String)
            _myID = value
        End Set
    End Property
    
    Public Property Supplier() As String
        Get
            Return _supplier
        End Get
        Set(ByVal value As String)
            _supplier = value
        End Set
    End Property End Class
    
    1. 代码JQuery / Json的客户端
    2.         

                  

      <div id="output"></div>
      <script type="text/javascript">
      $(function () {
      
       $('#Button1').click(getRates);
      
       });
      
      
      
      function getRates() {
      
      $.ajax({
      
          type: "POST",
      
          url: "Hotelrates.asmx/GetRates",
      
          data: "{}",
      
          contentType: "application/json; charset=utf-8",
      
          dataType: "json",
      
          success: function (response) {
      
              var rates = response.d;
      
              $('#output').empty();
      
              $.each(rates, function () {
      
                  $('#output').append('<p><strong>' + Rate.myID + ' ' +
      
                              Rate.myID + '</strong><br /> Year: ' +
      
                              Rate.mySupplier + '<br />');
      
              });
      
          },
      
          failure: function (msg) {
      
              $('#output').text(msg);
      
          }
      });
           }
      
        </script>
        </asp:Content>
      

1 个答案:

答案 0 :(得分:2)

您的js中的Rate变量似乎未声明。

尝试使用以下方法更改成功回调函数:

success: function (response) {
    var rates = response.d;
    $('#output').empty();

    for (var r in rates) {
        $('#output').append('<p><strong>' + rates[r].myID + ' ' +
                    rates[r].myID + '</strong><br /> Year: ' +
                    rates[r].mySupplier + '<br />');

    }
}