DataTables不显示数据 [英] DataTables does not display data

查看:143
本文介绍了DataTables不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery DataTables 控件.问题是我无法显示数据.

I'm trying the jQuery DataTables control. The problem is that I cannot display data.

HTML是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataTablesTest.aspx.cs" Inherits="JsonTest.DataTablesTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>DataTables Test</title>
  <script src="Scripts/jquery-1.10.2.min.js"></script>
  <link href="Content/DataTables/css/jquery.dataTables.css" rel="stylesheet" />
  <script src="Scripts/DataTables/jquery.dataTables.js"></script>
  <link href="Content/Site.css" rel="stylesheet" />
</head>

<script>
  var d = [
    { "Id": 3, "ActivityID": 91, "OperationType": 0, "UserID": 4183, "Comments": "", "ActionDate": "2006-03-19T12:26:01.673" },
    { "Id": 4, "ActivityID": 91, "OperationType": 4, "UserID": 4183, "Comments": "", "ActionDate": "2006-03-19T12:26:01.673" },
    { "Id": 5, "ActivityID": 92, "OperationType": 0, "UserID": 5688, "Comments": "", "ActionDate": "2006-03-20T12:05:40.563" }
  ];

  $(document).ready(function () {

    $('#example').dataTable({
      "ajax": {
        "url": "WebService1.asmx/GetData",
        "type": "POST",
        "dataSrc": "",
        "contentType": "application/json; charset=utf-8"
      },
      //"data": d,
      "columns": [
          { "data": "Id" },
          { "data": "ActivityID" },
          { "data": "OperationType" },
          { "data": "UserID" },
          { "data": "Comments" },
          { "data": "ActionDate" }
      ]
    });


    var table = $('#example').DataTable();
    alert('There are' + table.data().length + ' row(s) of data in this table');

  });
</script>

<body>
  <form id="form1" runat="server">
    <div>

      <table id="example" class="display">
        <thead>
          <tr>
            <th>ActivityHistoryID</th>
            <th>AH_ActivityID</th>
            <th>AH_OperationType</th>
            <th>AH_UserID</th>
            <th>AH_Comments</th>
            <th>AH_TimeStamp</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>
  </form>
</body>
</html>

如果我注释掉Ajax代码并取消注释

If I comment out the Ajax code and uncomment the

//"data": d,

工作正常. d变量是我使用firefox-> developer-> network-> xhr->响应对话框从服务获取的JSON数据.

It works fine. The d variable is the JSON data that I get from the service using firefox->developer->network->xhr->response dialog.

我阅读了很多帖子,尝试了很多事情,但无法使其正常工作. 有什么帮助吗? 谢谢.

I read many posts and I tried many things but I can't make it work. Any help? Thanks.

服务代码:

namespace JsonTest
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetData()
        {
            var list = ActivityHistory.GetAll(); // List<ActivityHistory>
            var s = Newtonsoft.Json.JsonConvert.SerializeObject(list);

            return s;
            //return "{\"aaData\":" + s + "}";
        }
    }
}

编辑2015年7月21日: 我已经对HTML代码进行了一些更改,并且正在处理一个小错误.加载时,我在页面顶部有一会儿看到表格元素的标题(ActivityHistoryID,AH_ActivityID,AH_OperationType,AH_UserID,AH_Comments,AH_TimeStamp).之后,它可以正常工作(用于60.000行!!!).

EDIT 21/07/2015: I've done some changes in HTML code and it's working with a little bug. While loading I see for a moment at the top of the page the headers of the table element (ActivityHistoryID, AH_ActivityID, AH_OperationType, AH_UserID, AH_Comments, AH_TimeStamp). After that it's working fine (for 60.000 rows!!!).

新更改的代码为:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataTablesTest.aspx.cs" Inherits="JsonTest.DataTablesTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>DataTables Test</title>
  <script src="Scripts/jquery-1.10.2.min.js"></script>
  <link href="Content/DataTables/css/jquery.dataTables.css" rel="stylesheet" />
  <script src="Scripts/DataTables/jquery.dataTables.js"></script>
  <link href="Content/Site.css" rel="stylesheet" />

  <script>

    $(document).ready(function () {

      $.ajax({
        type: "post",
        url: "WebService1.asmx/getdata",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
          var myData = $.parseJSON(data.d);

          $('#example').DataTable({
            "data": myData,
            "columns": [
                { "data": "Id" },
                { "data": "ActivityID" },
                { "data": "OperationType" },
                { "data": "UserID" },
                { "data": "Comments" },
                { "data": "ActionDate" }
            ]
          });
        }
      });

    });
</script>
</head>

<body>
  <form id="form1" runat="server">
    <div>
      <table id="example" class="display">
        <thead>
          <tr>
            <th>ActivityHistoryID</th>
            <th>AH_ActivityID</th>
            <th>AH_OperationType</th>
            <th>AH_UserID</th>
            <th>AH_Comments</th>
            <th>AH_TimeStamp</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>
  </form>
</body>
</html>

我的最后希望是我正在使用JQuery 1.10.2而不是datatables站点示例中的1.11.1.

My last hope is that I 'm using JQuery 1.10.2 instead of 1.11.1 which is in the examples of datatables site.

这是尝试的第三天,但我仍然无法弄清.

It 's the third day of trying and still I cannot figure it out.

编辑22/7/2015

那是有效的代码.远非示例.

That is the code that works. Far from examples.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataTablesTestWorking.aspx.cs" Inherits="JsonTest.DataTablesTestWorking" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>DataTables Test</title>
  <script src="Scripts/jquery-1.11.3.min.js"></script>
  <link href="Content/DataTables/css/jquery.dataTables.css" rel="stylesheet" />
  <script src="Scripts/DataTables/jquery.dataTables.js"></script>
  <link href="Content/Site.css" rel="stylesheet" />

  <script>


    $(document).ready(function () {
      $('#example').hide();

      $.ajax({
        type: "POST",
        url: "WebService1.asmx/GetData",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
          $('#example').show();

          var myData = $.parseJSON(data.d);

          $('#example').DataTable({
            "data": myData,
            "columns": [
                { "data": "Id" },
                { "data": "ActivityID" },
                { "data": "OperationType" },
                { "data": "UserID" },
                { "data": "Comments" },
                { "data": "ActionDate" }
            ]
          });
        }
      });


    });
  </script>
</head>

<body>
  <form id="form1" runat="server">
    <div>
      <table id="example" class="display">
        <thead>
          <tr>
            <th>ActivityHistoryID</th>
            <th>AH_ActivityID</th>
            <th>AH_OperationType</th>
            <th>AH_UserID</th>
            <th>AH_Comments</th>
            <th>AH_TimeStamp</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>
  </form>
</body>
</html>

推荐答案

根据

According to this post option contentType: 'application/json; charset=UTF-8' and type: 'POST' is indeed required to invoke an ASP.NET AJAX web method.

但是,为了发送JSON编码的参数而不是URL编码的参数,您需要使用 ajax.data 选项可将参数编码为JSON格式的字符串.

However, in order to send JSON-encoded parameters rather than URL-encoded parameters, you need to use ajax.data option to encode parameters into string in JSON format.

$('#example').dataTable({
  "ajax": {
    "url": "WebService1.asmx/GetData",
    "type": "POST",
    "contentType": "application/json; charset=utf-8",
    "dataType": "json",
    "data": function ( d ) {
       return JSON.stringify( d );
    },
    "dataSrc": "d",
  },
  "columns": [
      { "data": "Id" },
      { "data": "ActivityID" },
      { "data": "OperationType" },
      { "data": "UserID" },
      { "data": "Comments" },
      { "data": "ActionDate" }
  ]
});

这篇关于DataTables不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆