2015-09-02 2 views
0

Я использую jQuery DataTable для заполнения таблицы как запрос AJAX веб-методом C#. Моя проблема - не возвращать никакие данные или ошибки. Может кто-нибудь мне помочь?jquery datatable issue no data retrieved

JS код:

var TableAjax = function() { 

    var initPickers = function() { 
     //init date pickers 
     $('.date-picker').datepicker({ 
      rtl: Metronic.isRTL(), 
      autoclose: true 
     }); 
    } 

    var handleRecords = function() { 
    var grid = new Datatable(); 
    grid.init({ 
     src: $("#datatable_ajax"), 
     onSuccess: function (grid) { 
      // execute some code after table records loaded 
     }, 
     onError: function (grid) { 
      // execute some code on network or other general error 
     }, 
     onDataLoad: function (grid) { 
      // execute some code on ajax data load 
     }, 
     loadingMessage: 'Loading...', 
     DataTable: { // here you can define a typical datatable settings from http://datatables.net/usage/options 
      // Uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout 
      // setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/scripts/datatable.js). 
      // So when dropdowns used the scrollable div should be removed. 
      //"dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>>r>t<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>", 

      "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie. 

      "lengthMenu": [ 
       [10, 20, 50, 100, 150, -1], 
       [10, 20, 50, 100, 150, "All"] // change per page values here 
      ], 
      "pageLength": 10, // default record count per page 
      "ajax": { 
       url: "/WS/WS.asmx/GetCustomers", 
       type: 'GET', 
       dataType: 'json', 
       contentType: 'application/json; charset=utf-8', 

      }, 
      "aoColumns": [ 
       { "mData": "CustomerAddress" }, 
       { "mData": "CustomerName" }, 
       { "mData": "CustomerCode" }, 
       { "mData": "CustomerCode" }, 
       { "mData": "CustomerCode" }, 
       { "mData": "CustomerCode" }, 
       { "mData": "CustomerCode" } 
      ], 
      "order": [ 
       [1, "asc"] 
      ]// set first column as a default sort by asc 
     } 
    }); 

    // handle group actionsubmit button click 
    grid.getTableWrapper().on('click', '.table-group-action-submit', function (e) { 
     e.preventDefault(); 
     var action = $(".table-group-action-input", grid.getTableWrapper()); 
     if (action.val() != "" && grid.getSelectedRowsCount() > 0) { 
      grid.setAjaxParam("CustomerId", "group_action"); 
      grid.setAjaxParam("CustomerName", action.val()); 
      grid.setAjaxParam("id", grid.getSelectedRows()); 
      grid.getDataTable().ajax.reload(); 
      grid.clearAjaxParams(); 
     } else if (action.val() == "") { 
      Metronic.alert({ 
       type: 'danger', 
       icon: 'warning', 
       message: 'Please select an action', 
       container: grid.getTableWrapper(), 
       place: 'prepend' 
      }); 
     } else if (grid.getSelectedRowsCount() === 0) { 
      Metronic.alert({ 
       type: 'danger', 
       icon: 'warning', 
       message: 'No record selected', 
       container: grid.getTableWrapper(), 
       place: 'prepend' 
      }); 
     } 
    }); 
} 

return { 
    //main function to initiate the module 
    init: function() { 
     initPickers(); 
     handleRecords(); 
    } 
}; 

}(); 

и мой код веб-метод:

[WebMethod(EnableSession = true)] 
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
public object GetCustomers() 
{ 
    DataTable dt = ICMS_DB_CLS.GetDataFromDB("Select * from t_crm_customers"); 
    List<Customers> lis = new List<Customers>(); 
    lis = ExtensionList.ToListof<Customers>(dt); 
    return new 
    { 
     data = lis, 
     length = lis.Count 
    }; 
} 

Моя проблема не возвращает никаких данных или каких-либо ошибок.

ответ

0

На данный момент вы просто возвращаете объект.

Try:

[WebMethod(EnableSession = true)] 
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
public JsonResult GetCustomers() 
{ 
    DataTable dt = ICMS_DB_CLS.GetDataFromDB("Select * from t_crm_customers"); 
    List<Customers> lis = new List<Customers>(); 
    lis = ExtensionList.ToListof<Customers>(dt); 
    var toJson = new 
     { 
      data = lis, 
      length = lis.Count 
     }; 
    return Json(toJson, JsonRequestBehaviour.AllowGet); 
}