2017-07-28 1 views
0

ich benutze asp.net mvc, möchte Json Daten von der Serverseite laden. Ich habe dieses Stück serverseitigen Code:jQuery Datatables laden Daten vom Server

Function GetData() As ActionResult 

      Dim TransactionSearchRow1 = New TransactionSearchRow With { 
      .status = Status.Cancelled, 
      .transactinId = 12345, 
      .creditCardNumber = "1234324324", 
      .supplier = "Office Depot", 
      .createdAt = New DateTime(2008, 12, 28), 
      .amount = 500 
      } 

      Dim TransactionSearchRowJson = JsonConvert.SerializeObject(TransactionSearchRow1) 

      Return Json(TransactionSearchRowJson) 

     End Function 

Seine senden Sie mir nur json String aus einem TransactionSearchRow Objekt zurück.

Ich habe, dass clientseitigen Code:

$("#searchBTN").on("click", function() { 
       $.ajax({ 
        url: '/Transaction/GetData', 
        method: 'POST', 
        dataType: 'json', 
        success: function (data) { 
         $('#TransactionTable').dataTable({ 
          data: data, 
          columns: [ 
           { 'data': 'status' }, 
           { 'data': 'transactinid' }, 
           { 'data': 'creditcardnumber' }, 
           { 'data': 'supplier' }, 
           { 'data': 'createdAt' }, 
           { 'data': 'amount' } 
          ] 

         }); 

        } 
       }); 
      }); 

Und einfache HTML-Tabelle:

<table id="TransactionTable" class="table table-striped table-bordered table-list"> 
        <thead> 
         <tr> 
          <th class="col-md-1">Status</th> 
          <th>TransID</th> 
          <th>CCN</th> 
          <th>Supplier</th> 
          <th>Created Date</th> 
          <th>Amount</th> 
         </tr> 
        </thead> 

        <tbody> 

        </tbody> 
       </table> 

JSON Antwort: enter image description here

Aber ich erhalte eine Fehlermeldung, wenn im Klick auf „Suchen " Taste. enter image description here

enter image description here

+0

Teilen Sie Ihre Json Antwort –

+0

es auf Post hinzufügen. –

+0

haben Sie bestätigt, dass 'data' in der Erfolgsfunktion korrekt zurückgegeben wird? –

Antwort

2

Ich denke, Ihre Antwort-Datenformat ist nicht richtig. bitte referenzieren Sie diese example. Die Ajax-Antwort sollte ein Objekt sein, das eine Array-Eigenschaft namens "Daten" hat. Ich bin nicht vertraut mit VB, also gebe ich Ihnen C# Code-Snippet. Ihre Server-Seite Aktionscode ändern wie folgt vor:

return Json(new { 
     data: new List<TransactionSearchRow>(){TransactionSearchRow1} 
    }) 
+0

Wird es versuchen und zurück –