代码之家  ›  专栏  ›  技术社区  ›  sipsorcery

将DataTables jQuery插件与WCF服务一起使用的示例

  •  2
  • sipsorcery  · 技术社区  · 14 年前

    有人知道使用 DataTables 带wcf服务的jquery插件?

    我正在尝试将wcf服务与javascriptserializer一起使用,但不幸的是,它似乎通过添加额外的反斜杠返回了不可靠的JSON。然而,数据表似乎提供了一种解决这一问题的方法,因为JSON的检索可以移交给jquery调用。不过,我对jquery还不够熟悉,无法让它正常工作。

    我的javascript是:

        $(document).ready(function () {
            $('#example').dataTable({
                "bJQueryUI": true,
                "bSort": true,
                "bProcessing" : true,
                "bServerSide" : true,
                "bAutoWidth": true,
                "sAjaxSource": "http://10.1.1.7/mvc-jqdatatable/datatabletestservice.svc/gettable",
                "fnServerData": function(sSource, aoData, fnCallback) {
                    $.getJSON( sSource, aoData, function (json) { 
                            fnCallback(json)
                    } )
                },
            });
        });
    

    我的WCF服务正在吐:

    HTTP/1.1 200 OK
    Content-Length: 56
    Content-Type: application/json; charset=utf-8
    Server: Microsoft-IIS/7.5
    X-Powered-By: ASP.NET
    Date: Thu, 23 Sep 2010 12:37:24 GMT
    
    "{\"aaData\":[[\"a\",\"b\",\"c\"],[\"d\",\"e\",\"f\"]]}"
    

    json字符串正在进入datattables脚本,但它没有被识别为json,错误为:

    “aadata.length”为空或不是对象

    1 回复  |  直到 14 年前
        1
  •  1
  •   sipsorcery    14 年前

    墨菲定律,我一提出这个问题就发现 sample 这让我振作起来。

    最后的诀窍是使用jquery解析wcf服务返回的字符串。如果不这样做,datatables脚本就无法理解wcf使用的JSON格式,因为它要么是非标准的,要么是在推动边界。

        $(document).ready(function () {
            $('#example').dataTable({
                "bJQueryUI": true,
                "bSort": true,
                "bProcessing" : true,
                "bServerSide" : true,
                "bAutoWidth": true,
                "sAjaxSource": "http://10.1.1.7/mvc-jqdatatable/datatabletestservice.svc/gettable",
                "fnServerData": function(sSource, aoData, fnCallback) {
                    $.ajax({
                      "datatType" : 'json',
                      "contentType" : 'application/json',
                      "url" : sSource,
                      "data" : aoData,
                      "success" : function(msg) {
                        var json = $.parseJSON(msg);
                        fnCallback(json);
                      }
                    })
                },
            });
        });
    

    与以下WCF服务一起工作:

    public interface IDataTableTestService
    {
        [OperationContract]
        [WebInvoke(ResponseFormat=WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest, Method="GET")]
        string GetTable(int iDisplayStart,
            int iDisplayLength,
            string sSearch,
            bool bEscapeRegex,
            int iColumns,
            int iSortingCols,
            int iSortCol_0,
            string sSortDir_0,
            int sEcho,
            int webSiteId,
            int categoryId);
    }
    
    public class DataTableTestService : IDataTableTestService
    {
        public string GetTable(int iDisplayStart,
             int iDisplayLength,
             string sSearch,
             bool bEscapeRegex,
             int iColumns,
             int iSortingCols,
             int iSortCol_0,
             string sSortDir_0,
             int sEcho,
             int webSiteId,
             int categoryId)
        {
    
            var result = new List<string[]>() { new string[]{"a", "b", "c"}, new string[]{"d", "e", "f"}};
    
            JavaScriptSerializer serialiser = new JavaScriptSerializer();
            return serialiser.Serialize(new {  sEcho,
                                                iTotalRecords = 2,
                                                iTotalDisplayRecords = 2,
                                                aaData = result
                                            });
        }
    }