[英]How to fill a jQuery DataTable with AJAX-requested JSON data?
Im trying to use jQuery Datatables filled by a AJAX-requested json.txt
but for some reason my file does not load into my datatable. 我试图使用由AJAX请求的
json.txt
填充的jQuery数据表,但是由于某种原因,我的文件未加载到我的数据表中。
Why is my datatable not filling correctly? 为什么我的数据表无法正确填充? Do I have some syntax error?
我有语法错误吗?
This is my datatable syntax: 这是我的数据表语法:
$(document).ready(function () {
var table = $('#pftable_hdr').DataTable({
"ajax": {
"url": "/path.txt",
"dataSrc": ""
},
"columns": [
{ "data": "Fecha" },
{ "data": "Rid" },
{ "data": "Pdv" },
{ "data": "Pla" },
{ "data": "Descripcion" },
{ "data": "Total" },
{ "data": "Cantidad" }
],
scrollY: "500px",
scrollX: true,
scrollCollapse: true,
fixedColumns: {
leftColumns: 3
}
});
});
and this is the HTML for my table: 这是我的表格的HTML:
<table class="table table-hover no-more-tables table-iconmebanquet-detail" id="pftable_hdr">
<thead>
<tr>
<th style="">Fecha</th>
<th style="">Rid</th>
<th style="">Pdv</th>
<th style="">Pla</th>
<th style="">Descripcion</th>
<th style="">Total</th>
<th style="">Cantidad</th>
</tr>
</thead>
<tfoot>
<tr>
<th style="">Fecha</th>
<th style="">Rid</th>
<th style="">Pdv</th>
<th style="">Pla</th>
<th style="">Descripcion</th>
<th style="">Total</th>
<th style="">Cantidad</th>
</tr>
</tfoot>
</table>
And this is my JSON file at the correct route: 这是我在正确路径下的JSON文件:
[
{
"Fecha": "/Date(1451631600000)/",
"Rid": "CENAS",
"Pdv": "REST",
"Pla": "81",
"Descripcion": "ENS FRUTAS",
"Total": 53.0000,
"Cantidad": 1
},
{
"Fecha": "/Date(1451631600000)/",
"Rid": "CENAS",
"Pdv": "REST",
"Pla": "87",
"Descripcion": "CAFE AMER ILIMIT",
"Total": 23.0000,
"Cantidad": 1
},
{
"Fecha": "/Date(1451631600000)/",
"Rid": "CENAS",
"Pdv": "REST",
"Pla": "97",
"Descripcion": "QUESADILLAS TRAD (2)",
"Total": 51.0000,
"Cantidad": 1
},
{
"Fecha": "/Date(1451631600000)/",
"Rid": "CENAS",
"Pdv": "VTSI",
"Pla": "114",
"Descripcion": "ENS CHIAPANECA",
"Total": 65.0000,
"Cantidad": 1
},
{
"Fecha": "/Date(1451631600000)/",
"Rid": "COMIDAS",
"Pdv": "VTMO",
"Pla": "196",
"Descripcion": "POZ ROJO SUP MAC",
"Total": 91.0000,
"Cantidad": 1
}
]
The url to your textfile in your source code is wrong: 源代码中文本文件的URL错误:
"url": "C:\inetpub\wwwroot\potzolcalli.brain.arpon.com\path.txt",
In JavaScript, a backslash \\
is an escape character for strings, such as a newline character: "\\n"
. 在JavaScript中,反斜杠
\\
是字符串的转义字符,例如换行符"\\n"
。
You should either use a file:// url: 您应该使用file://网址:
"url": "file://C:/inetpub/wwwroot/potzolcalli.brain.arpon.com/path.txt",
or use a local http server to serve your files, eg http-server , assuming you have node installed: 或使用本地http服务器来提供文件,例如http-server ,前提是您已安装节点 :
npm install -g http-server
cd <root directory of your files, e.g. your index.html>
http-server
You can then open your browser at http://localhost:8080 to consume your site similar to the way they will get served by an http server in production later. 然后,您可以在http:// localhost:8080上打开浏览器以使用您的站点,这与以后在生产中由http服务器提供服务的方式类似。
UPDATED: i solved the proble changing my Ip by the suggestion of @Leon Adles so i made the next correction and it dysplayed perfectly now 更新:我通过@Leon Adles的建议解决了更改Ip的问题,因此我进行了下一次更正,现在它表现得很完美
"url": "/path.txt", “ url”:“ /path.txt”,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.