如何将JSON数据绑定到JQUERY Datatable并使用Ajax刷新它?

时间:2018-12-04 11:21:18

标签: jquery json ajax vb.net datatable

我正在尝试使用JSON和Ajax将SQL Server数据与Jquery datable连接。但这不起作用。

我已经使用web方法生成了JSON数据 如下

<WebMethod()> _
Public Function GetQueryInfo() As String
    Dim Jsonresult As String = Nothing     
    Dim ds As DataSet = New DataSet()
    Dim dt As New DataTable()
    Dim constr As String = Constr
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("Select * from COMPLAINTTYPE", con)
            con.Open()
            Dim MyDataReader As SqlDataReader = cmd.ExecuteReader()
            dt.Load(MyDataReader)
            Jsonresult = GetJson(dt)
            Return Jsonresult
        End Using
        con.Close()
    End Using
End Function
 Public Function GetJson(ByVal dt As DataTable) As String
    Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim rows As New List(Of Dictionary(Of String, Object))
    Dim row As Dictionary(Of String, Object)
    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)
        For Each col As DataColumn In dt.Columns
            row.Add(col.ColumnName, dr(col))
        Next
        rows.Add(row)
    Next
    Return serializer.Serialize(rows)
End Function

我在调用如下Web方法时得到JSON

[{"ID":89,"COMPLAINTTYPE":"Motherboard complaint","CTYPE_ABS":"Motherboardcomplaint"},
{"ID":90,"COMPLAINTTYPE":"CMOS batter not working","CTYPE_ABS":"CMOSbatternotworking"},
{"ID":91,"COMPLAINTTYPE":"Memory damaged","CTYPE_ABS":"Memorydamaged"},
{"ID":92,"COMPLAINTTYPE":"SMPS damaged","CTYPE_ABS":"SMPSdamaged"},
{"ID":93,"COMPLAINTTYPE":"97472","CTYPE_ABS":"97472"},
{"ID":94,"COMPLAINTTYPE":"6635","CTYPE_ABS":"6635"},{"ID":95,"COMPLAINTTYPE":"new
omplaintfjjf","CTYPE_ABS":"newomplaintfjjf"}]

这是我的HTML标记

<script type="text/javascript">
        $(document).ready(function () {
            $('#eg1').DataTable({
                "ajax": {
                    "url": "DataJason.asmx/GetQueryInfo",
                    "type": "Get",
                    "datatype": "Json",
                },
                "Columns": [
                   { title: "ID" },
                   { title: "CTYPE" },
                   { title: "CTYPE_ABS" }
                ]

            });
        });
    </script>
 <table id="eg1"   class="table table-bordered table-striped mb-none">
        <thead> <asp:Button ID="btnPopup" runat="server" Text="Add new Complaints"  CssClass="mb-xs mt-xs mr-xs btn btn-xs btn-primary"/>
            <tr>
            <th>ID</th>
            <th>Complaint</th>
            <th>Ctype</th>
            </tr>
        </thead>
    </table>

我收到此错误警报“ DataTables警告:表ID = eg1-请求的第0行的未知参数'ID'。有关此错误的更多信息,请参见datatables.net/tn/4”,因此我对此进行了检查链接datatables.net/manual/tech-notes/4,它说字段ID丢失了,但是JSON返回了ID 您能解释一下为什么这行不通吗?

0 个答案:

没有答案