Javascript 在jQuery中将AJAX JSON调用与动态表合并
我对JavaScript有点陌生。我有一个ajax请求,它获取json数据并将其放入表中,并尝试集成dynatable.js。然而,我不确定最好的方法来做到这一点,因为我需要两者 当前JS:Javascript 在jQuery中将AJAX JSON调用与动态表合并,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我对JavaScript有点陌生。我有一个ajax请求,它获取json数据并将其放入表中,并尝试集成dynatable.js。然而,我不确定最好的方法来做到这一点,因为我需要两者 当前JS: $(function () { 'use strict'; //global var current_file_index = 0; // Initialize the jQuery File Upload widget: $('#fileupload').fil
$(function () {
'use strict';
//global
var current_file_index = 0;
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: 'server/php/',
prependFiles:true
}).on('fileuploadsubmit', function (e, data) {
//data.formData = {date: data.files[0].date};
data.formData = data.context.find(':input').serializeArray();
});
// Load existing files:
$('#fileupload').addClass('fileupload-processing');
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $('#fileupload').fileupload('option', 'url'),
dataType: 'json',
context: $('#fileupload')[0],
prependFiles:true,
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
});
});
动态表JS:
$('#remote').dynatable({
dataset: {
ajax: true,
ajaxOnLoad: true,
ajaxUrl: '/server/php/index.php',
files: []
}
});
如果我同时使用这两种方法,我会得到:
未捕获的TypeError:无法读取null的属性“length”
at Records.count(jquery.dynatable.js:694)
在recordscont.create(jquery.dynatable.js:708)
在recordscont.attach(jquery.dynatable.js:734)
在recordscont.init(jquery.dynatable.js:704)
在Object.build(jquery.dynatable.js:186)
在Object.init(jquery.dynatable.js:142)
在HTMLTableElement。(jquery.dynatable.js:1673)
在Function.each(jquery.min.js:2)
在n.fn.init.each(jquery.min.js:2)
位于n.fn.init.$.fn.dynatable(jquery.dynatable.js:1671)
如果我做了下面的工作,但只是添加了搜索、0中的0等。我尝试对表进行排序,并删除通过ajax加载的所有数据。总而言之,我认为我需要更好地集成它们,以便它们能够协同工作
$('#remote').dynatable();
对于想要解决此问题的任何人,请参见以下内容:
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $('#fileupload').fileupload('option', 'url'),
dataType: 'json',
context: $('#fileupload')[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
$('.table-striped').dynatable({
dataset: {
files: result,
dataType: 'text'
}
});
});
有什么问题?这里有什么问题吗?您确定得到了json响应吗?当我直接转到页面时,我的json响应是正确的,ajax请求可以很好地加载它。设法解决了,我会马上发布解决方案