Sto provando a utilizzare la funzionalità lato server del plugin jQuery Datatables con ASP.Net. La richiesta Ajax restituisce JSON valido, ma nella tabella non compare nulla.jQuery DataTables elaborazione lato server e ASP.Net
Originariamente avevo problemi con i dati che stavo inviando nella richiesta Ajax. Stavo ricevendo un errore "primitivo JSON non valido". Ho scoperto che i dati devono essere in una stringa anziché in JSON serializzati, come descritto in questo post: http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/. Non ero del tutto sicuro come risolvere il problema che, così ho provato ad aggiungere questo nella richiesta Ajax:
"data": "{'sEcho': '" + aoData.sEcho + "'}"
Se i aboves fine lavori io aggiungo gli altri parametri in seguito. In questo momento sto solo cercando di ottenere qualcosa da mostrare nel mio tavolo.
Il JSON restituito sembra ok e convalida, ma il sEcho nel post non è definito, e penso che questo sia il motivo per cui nessun dato viene caricato nella tabella.
Quindi, cosa sto sbagliando? Sono sulla buona strada o sono stupido? Qualcuno si è imbattuto in questo prima o ha qualche suggerimento?
Ecco la mia 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, 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
restituito JSON:
{"__type":"Data","sEcho":0,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[["testing","chad","testing"],["testing2","chad","testing"]]}
hai mai ottenuto il filtraggio, il paging e l'ordinamento per funzionare? –