jquery-plugins – jQuery DataTables服务器端处理和ASP.Net

前端之家收集整理的这篇文章主要介绍了jquery-plugins – jQuery DataTables服务器端处理和ASP.Net前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用ASP.Net的jQuery Datatables插件的服务器端功能. ajax请求返回有效的 JSON,但表中没有显示任何内容.

我最初在ajax请求中发送的数据有问题.我得到一个“无效的JSON主题错误.我发现数据需要是一个字符串,而不是JSON序列化,如本文中所述:http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/.我不太确定如何解决这个问题,所以我尝试在ajax请求中添加

"data": "{'sEcho': '" + aoData.sEcho + "'}"

如果上面的内容最终有效,我稍后再添加其他参数.现在我只是想在桌子上出现一些东西.

返回的JSON看起来确定并验证,但是post中的sEcho是未定义的,我认为这就是为什么没有数据被加载到表中.

那么我做错了什么?我甚至在正确的轨道上,还是我是愚蠢的?有人有人遇到过这个或有什么建议吗?

这是我的jQuery:

$(document).ready(function()
{

    $("#grid").dataTable({
            "bJQueryUI": true,"sPaginationType": "full_numbers","bServerSide":true,"sAjaxSource": "GridTest.asmx/ServerSideTest","fnServerData": function(sSource,aoData,fnCallback) {
               $.ajax({
                    "type": "POST","dataType": 'json',"contentType": "application/json; charset=utf-8","url": sSource,"data": "{'sEcho': '" + aoData.sEcho + "'}","success": fnCallback
                });
           }
         });


 });

HTML:

<table id="grid">
   <thead>
      <tr>
         <th>Last Name</th>
         <th>First Name</th>
         <th>UserID</th>
       </tr>
    </thead>

    <tbody>
       <tr>
    <td colspan="5" class="dataTables_empty">Loading data from server</td>
       </tr>
    </tbody>
 </table>

WEBMETHOD:

<WebMethod()> _
Public Function ServerSideTest() As Data


    Dim list As New List(Of String)
    list.Add("testing")
    list.Add("chad")
    list.Add("testing")

    Dim container As New List(Of List(Of String))
    container.Add(list)

    list = New List(Of String)
    list.Add("testing2")
    list.Add("chad")
    list.Add("testing")

    container.Add(list)

    HttpContext.Current.Response.ContentType = "application/json"

    Return New Data(HttpContext.Current.Request("sEcho"),2,container)

End Function


Public Class Data
Private _iTotalRecords As Integer
Private _iTotalDisplayRecords As Integer
Private _sEcho As Integer
Private _sColumns As String
Private _aaData As List(Of List(Of String))

Public Property sEcho() As Integer
    Get
        Return _sEcho
    End Get
    Set(ByVal value As Integer)
        _sEcho = value
    End Set
End Property

Public Property iTotalRecords() As Integer
    Get
        Return _iTotalRecords
    End Get
    Set(ByVal value As Integer)
        _iTotalRecords = value
    End Set
End Property

Public Property iTotalDisplayRecords() As Integer
    Get
        Return _iTotalDisplayRecords
    End Get
    Set(ByVal value As Integer)
        _iTotalDisplayRecords = value
    End Set
End Property



Public Property aaData() As List(Of List(Of String))
    Get
        Return _aaData
    End Get
    Set(ByVal value As List(Of List(Of String)))
        _aaData = value
    End Set
End Property

Public Sub New(ByVal sEcho As Integer,ByVal iTotalRecords As Integer,ByVal iTotalDisplayRecords As Integer,ByVal aaData As List(Of List(Of String)))
    If sEcho <> 0 Then Me.sEcho = sEcho
    Me.iTotalRecords = iTotalRecords
    Me.iTotalDisplayRecords = iTotalDisplayRecords
    Me.aaData = aaData
End Sub

已返回的JSON:

{"__type":"Data","sEcho":0,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[["testing","chad","testing"],["testing2","testing"]]}

解决方法

我更改了数据
"data": "{'sEcho': '"+ aoData[0].value + "'}",

它工作.所以现在的问题是如何将其余的数据传递给webservice.我尝试使用JSON2将JSON转换为字符串,但是这又打开了另一个蠕虫病毒,这是一个单独的问题.

猜你在找的jQuery相关文章