对于我的生活,我无法理解这一点。
设定:
page 1: add.php
page 2: upload.php
page 3: return.php
在第一页上,用户从Excel上传spreadhseet:
<div id="return"></div>
~~~~~
$(document).ready(function (e) {
$("#uploadForm").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
jsonResponse = data;
$("#return").load("return.php")
}
});
}));
});
将Excel表格发送到upload.php
,并返回JSON响应以及来自单元格的数据。
echo json_encode($out);
这正是它在console.log
jsonResponse = [
{"dateReceived":"2015-01-01","designCustomer":"MULTITEST 1","designCustomerLocation":"SUNNYVALE, CA"},
{"dateReceived":"2016-04-05","designCustomer":"MULTITEST 2","designCustomerLocation":"SUNNYVALE, CA"},
{"dateReceived":"1982-04-18","designCustomer":"MULTITEST 3","designCustomerLocation":"SUNNYVALE, CA"}
]
成功时,return.php
被加载到第一页上存在的#return
div中,并尝试使用JSON输出构建dataTable
...:
var table = $('#ltc-table').DataTable( {
"data" : jsonResponse,
"columns" : [
{ data : 'designCustomer' },
{ data : 'designCustomerLocation' },
{ data : 'dateReceived' }
]
});
但是,我收到错误:Uncaught Error: DataTables warning: table id=ltc-table - Requested unknown parameter 'designCustomer' for row 0.
我不明白的事情:
jsonResponse
是一个包含JSON的变量,当我在 console.log(jsonResponse);
上使用return.php
时,我会得到上面粘贴的确切字符串(所以我假设jsonResponse
是console.log
报告的变量,我可以假设return.php
是一个变量,但是,数据表说它无法找到变量,因为它发出了这个错误。
如果在jsonResponse
上,我创建了新的代码,而代之以jsonResponse = [
{"dateReceived":"2015-01-01","designCustomer":"MULTITEST 1","designCustomerLocation":"SUNNYVALE, CA"},
{"dateReceived":"2016-04-05","designCustomer":"MULTITEST 2","designCustomerLocation":"SUNNYVALE, CA"},
{"dateReceived":"1982-04-18","designCustomer":"MULTITEST 3","designCustomerLocation":"SUNNYVALE, CA"}
];
:
{{1}}
可行。
我做错了什么?这是一个问题,我将数据从一个页面传递到另一个页面加载到第一页上的div?这让我发疯了......
答案 0 :(得分:1)
你的ajax回复会不会回复文字呢?我没有在ajax中看到任何“json”类型。
data属性是否支持JSON。还是需要javascript对象? 查看“ajax sourced”下的文档,它意味着使用数据表函数的“ajax”属性: 例如 Ajax Sourced Datatable