jquery - 将数据表 jquery 插件与 WCF 服务结合使用的示例

标签 jquery wcf json datatables

有谁知道有任何使用 DataTables 的样本吗?带有 WCF 服务的 jquery 插件?

我正在尝试将 WCF 服务与 JavaScriptSerializer 一起使用,不幸的是,它似乎通过添加额外的反斜杠返回了狡猾的 JSON。然而,鉴于 JSON 的检索可以交给 jQuery 调用,DataTables 似乎提供了一种解决该问题的方法。我对 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' is null or not an object

最佳答案

墨菲定律,我一发布问题就发现了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
                                        });
    }
}

关于jquery - 将数据表 jquery 插件与 WCF 服务结合使用的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3778790/

相关文章:

c# - WCF 数据服务 $filter 在日期时间之间进行搜索

javascript - 传单:不同坐标的不同颜色标记

jquery - 如何使用 jQuery 将选中的 attr 添加到单选按钮 nth-child ?

python - 为什么我的 POST 在获取值后不起作用

javascript - 如何在 HTML 5 文档中添加 xml?

c# - 如何暂时关闭 WCF 客户端模拟以写入日志文件等

xml - IIS 关于 500 错误的通知

php - 试图从 mysql 获取 json 格式化程序

javascript - 使用 JSON 将 API 生成的 PID 添加到 URL

javascript - 将鼠标悬停在导航链接上时如何防止持续闪烁?