【发布时间】: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