数据表 - 无效的 JSON 响应查询 [英] DataTables - Invalid JSON Response Query
问题描述
我已经使用 .csv 文件(逗号分隔文件)作为数据源创建了一个页面,但是当我加载该页面时,返回了一个无效的 JSON 响应错误.当我检查网络 >XHR 我看不到任何错误信息,并且响应"选项卡下没有显示任何内容.但是,当我单击确定"按钮关闭错误消息时,.csv 文件中的所有数据都显示在响应"选项卡下.
I've created a page using a .csv file (comma delimited file) as the data source, but when I load the page an Invalid JSON response error is returned. When I check Network > XHR I can't see any error information and nothing is displayed under the Response tab. However, when I click on the OK button to dismiss the error message, all the data from the .csv file is displayed under the Response tab.
无论我在本地还是在我的网络服务器上托管文件,都会出现同样的问题.
The same issue results whether I host the files locally or on my webserver.
这可能是 https 中的配置问题://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js 文件?我在下面提供了相关的标头代码以供参考:
Could this be an issue with a configuration in the https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js file? I've provided the relevant header code I've used for reference below:
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
<style type="text/css" class="init"></style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" class="init">
$(document).ready(function() {
$('#mrgs').DataTable( {
"ajax": 'mydata.csv'
} );
} );
</script>
有没有人知道可能导致此问题的原因.任何帮助将不胜感激.
Does anyone have any ideas what could be causing this issue. Any assistance would be greatly appreciated.
推荐答案
您可以将 csv 预处理为 DataTable 想要的对象值数组格式.
You can preprocess your csv to be in the array of object values format DataTable wants.
这是可以插入您的页面的完整解决方案
Here is the complete solution ready to plug into your page
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href='https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css' />
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<table id="mrgs" class="table"></table>
<script>
$(document).ready(function() {
$.ajax({
url: "./data.csv",
context: document.body
}).done(function(csv) {
let allTextLines = csv.split(/\r\n|\n/);
let headers = allTextLines[0].split(',').map(e => e.trim());
let lines = [];
for (let i = 1; i < allTextLines.length; i++) {
let line = allTextLines[i].split(',') // take the comma separated line, turn into an array
.reduce((b, a, index) => { // b is the accumulator, a is the iteration value
b[headers[index].toLowerCase().replace(/\s+/g, '')] = a.trim(); // set the object property and value
return b; // return the accumulator for our next iteration
}, {});
if (Object.keys(line).length === headers.length) lines.push(line) }
// now fix headers to be object/value pairs
headers = headers.map((e) => ({
title: e,
data: e.toLowerCase().replace(/\s+/g, '')
}))
// console.log(headers)
// console.log(lines)
$('#mrgs').DataTable({
columns: headers,
data: lines
});
});
});
</script>
$(document).ready(function() {
let csv = `Type,Qtr,Year,Surname,Forenames,SpseName,District,Volume,Page
Marriages,Dec,1837,JAMES,Ann,,Mansfield,15,942
Marriages,Dec,1839,Karlton,Diana,,Mansfield,15,1017
Marriages,Dec,1841,Mepham,Elizabeth,,Mansfield,15,994
Marriages,Sep,1842,CASPIAN,Sophia,,Mansfield,15,617
Marriages,Dec,1842,Kennedy,Mark,,Mansfield,15,957
Marriages,Dec,1843,Crampus,Elizabeth,,Mansfield,15,1034
Marriages,Mar,1846,Dalton,Paulina,,Mansfield,15,741
Marriages,Dec,1846,JAMIESON,William,,Mansfield,15,1031
Marriages,Dec,1848,Rodon,Reuben,,Mansfield,15,1096
Marriages,Mar,1849,PHILBERT,Reuben,,Mansfield,15,703
Marriages,Dec,1849,STARKEY,Thos,,Mansfield,15,1092
Marriages,Jun,1850,Porter,John,,Mansfield,15,843`
let allTextLines = csv.split(/\r\n|\n/);
let headers = allTextLines[0].split(',').map(e => e.trim());
let lines = [];
for (let i = 1; i < allTextLines.length; i++) {
let line = allTextLines[i].split(',') // take the comma separated line, turn into an array
.reduce((b, a, index) => { // b is the accumulator, a is the iteration value
b[headers[index].toLowerCase().replace(/\s+/g, '')] = a.trim(); // set the object property and value
return b; // return the accumulator for our next iteration
}, {});
if (Object.keys(line).length === headers.length) lines.push(line)
}
// now fix headers to be object/value pairs
headers = headers.map((e) => ({
title: e,
data: e.toLowerCase().replace(/\s+/g, '')
}))
// console.log(headers)
// console.log(lines)
$('#mrgs').DataTable({
columns: headers,
data: lines
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href='//cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css' />
<script src="//cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<table id="mrgs" class="table"></table>
这篇关于数据表 - 无效的 JSON 响应查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!