【问题标题】:Can't load table from json using Javascript无法使用 Javascript 从 json 加载表
【发布时间】:2018-05-03 19:33:51
【问题描述】:

我想使用 Javascript 创建一个带有 JSON 的表,并从数据库中获取数据,并在我加载它时将其显示在我的页面上。该表必须有一个链接,可以将所选行中的数据放入文本框中。

为了填写表格,我在 .asmx 上使用了 方法

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
    public void CargarUsuarios()
    {
        var wcf = new wsDN.ServicioClient();
        var respuesta = wcf.Listar_Usuario().Valor.ToString();
        Context.Response.ContentType = "application/json";
        Context.Response.Write("{" + $"\"data\" : {respuesta}" + "}");
    }

这就是我创建表格的方式:

<table class="table table-bordered table-hover" id="tbl_Usuarios">
                            <thead>
                                <tr>
                                    <th>ID Usuario</th>
                                    <th>Cédula</th>
                                    <th>Nombre</th>
                                    <th>Apellidos</th>
                                    <th>Usuario</th>
                                    <th>Contraseña</th>
                                    <th>Opciones</th>
                                </tr>
                            </thead>
                        </table>

这就是 JS

<script src="js/main.js"></script>

            <script type="text/javascript">
                $(document).ready(function () {
                    $('#tbl_Usuarios').DataTable({
                        "language": {
                            "url": "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json"
                        },
                        "ajax": {
                            "url": "/Contenido.asmx/CargarUsuarios",
                            "type": "POST",
                            "dataType": "json"
                        },
                        "columnDefs": [
                            {
                                "targets": 6,
                                "orderable": false,
                                "render": function (data, type, row) {
                                    return '<a href="#">Modificar</a>';
                                }
                            }
                        ]
                    });
                });
</script>

【问题讨论】:

  • whta chrome 控制台 > 网络选项卡说关于发送请求(是否发送请求?响应是否包含数据?)
  • Uncaught TypeError: $(...).DataTable is not a function
  • 可能你需要一些库来将 DataTable 属性/函数添加到 jquery/html 元素(这个 jquery 插件datatables.net ?)
  • 同意卡米尔的观点。未找到数据表。确保它在你的脚本之前加载。

标签: javascript asp.net json


【解决方案1】:

可能您需要添加一些库,将 DataTable 属性/函数添加到 jquery/html 元素(您使用这个 jquery 插件 datatables.net 吗?)

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2021-08-02
    • 2018-04-05
    • 1970-01-01
    相关资源
    最近更新 更多