【发布时间】:2018-08-24 20:41:02
【问题描述】:
我正在尝试使用 AJAX 获取数据表中的子行数据:
$('#myTable tbody').on('click', 'td', function () {
var tr = $(this).closest('tr');
var row = myTable.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.remove();
tr.removeClass('shown');
}
else {
$.post('/salesLines',
{ token: localStorage.getItem('token'),
user: user.node,
id: localStorage.getItem('uniqueid')
})
.done(function(response) {
$.each(response.data, function (i, d) {
result += '<tr>'+'<td>'+d.qtysold+ ' ' + d.descr + ' ' + d.linetotal+'</td>'+'</tr>';
row.child( $(result) ).show(); // use selector $() for result to align child rows with main table column headings
tr.addClass('shown');
});
}
});
AJAX 请求似乎缓存了数据,即使它正在使用 $.post。
任何建议将不胜感激!
【问题讨论】:
-
$.post 未缓存。
Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.来自api.jquery.com/jQuery.post -
你试过简单的ajax吗? $.ajax({ url: "/myURL?", cache: false, });
-
你确定它是在缓存而不是“显示与以前相同的东西” - 我问的原因是我没有看到你实际上在任何地方更新 html,除了用它制作一个字符串 (
result)。 -
@James,是的,想知道
result += '<tr>'+'<td>'+d.qtysold+ '&nbsp;&nbsp;' + d.descr + '&nbsp;&nbsp;' + d.linetotal+'</td>'+'</tr>';应该做什么...... -
对不起,忘记了将结果附加到 DOM 的部分!尝试使用带有 cache: false 的普通 $.ajax 请求,结果是相同的。我应该使用 row.child.hide() 隐藏行吗?或 row.child.remove() ?
标签: javascript jquery node.js ajax datatables