简介:我正在使用ajax / json进行服务器端Jquery datatables.net库。我的示例项目可以找到:https://databasetable-net.000webhostapp.com/
错误:当我单击删除按钮时....控制台显示“未捕获的RangeError:超出最大调用堆栈大小”。它看起来好像没有发出Ajax调用(网络选项卡上没有显示) - 所以它必须在创建请求时。我怀疑我需要JSON.stringify我的del_id。
Index.php代码:
<script type="text/javascript">
$(document).ready(function() {
$( "#form1" ).hide();
$( "#signup" ).click( function() {
$( "#form1" ).toggle( 'slow' );
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "server.php",
"type": "POST",
},
columnDefs: [{
targets: -1,
defaultContent: '<button type="button" class="delete_btn" data-id=<?php echo "row[id]"?> ">Delete</button> <button type="button" class="edit_btn">Edit</button>'
}],
rowGroup: {
dataSrc: 1
}
});
});
</script>
<script type="text/javascript">
$(function(){
$(document).on('click','.delete_btn',function (e) {
e.stopPropagation();
var per_id=$(this).data('id');
var del_id= $(this).closest('tr');
var ele = $(this).parent().parent();
console.log(del_id);
$.ajax({
type:'POST',
url:'delete.php',
dataType: 'json', //This says I'm expecting a response that is json encoded.
data: { 'del_id' : del_id},
success: function(data){ //data is an json encoded array.
console.log('Data: ' + data);
console.log(JSON.stringify('Data: ' + data));
if(data['success']){ //You are checking for true/false not yes or no.
console.log('You successfully deleted the row.');
alert("row deleted");
ele.remove();
}else{
console.log('The row was not deleted.');
}
}
});
});
</script>
<script type="text/javascript">
$(document).on('click', '.edit_btn',function(){
var edit_id= $(this).closest('tr');
var ele = $(this).parent().parent(); //removed the "$" from the ele variable. It's a js variable.
console.log(edit_id);
$('#content-data').html('');
$.ajax({
url:'edit.php',
type:'POST',
data: { 'edit_id' : edit_id},
dataType:'html'
}).done(function(data){
$('#content-data').html('');
$('#content-data').html(data);
}).fail(function(){
$('#content-data').html('<p>Error</p>');
});
});
</script>
Delete.php
$del_id = $_POST['del_id'];
$stmt = $con->prepare("DELETE FROM employees WHERE id = ?"); //LIMIT 1
$stmt->bind_param('i', $del_id);
$confirmDelete = $stmt->execute();
$array['success'] = FALSE; //Initialize the success parameter as false.
if($confirmDelete){ //Check to see if there was an affected row.
$array['success'] = TRUE;
}
echo json_encode($array); //Your ajax is setup to expect a json response.
//json_encode the $array and echo it out. You have to do this.
//When you "echo" out a value, that is what the server is going to submit back to the ajax function.
//If you do not do this, the ajax script will not recieve a response from the delete.php page.
我尝试过:我尝试在delete.php页面中使用stringify.json但没有成功。
$array=JSON.stringify($array);
echo json_encode($array);
这似乎已经摆脱了Uncaught RangeError:最大调用堆栈大小超出错误:
$(document).on("click", ".remove-discount-button", function (e) {
e.stopPropagation();
//some code
});
主要的堆栈问题是由var edit_id = $(this).closest('tr');
引起的。您尝试将整个jQuery对象作为数据发送到ajax中。然后jQuery无法在内部对其进行序列化并抛出一个合适的东西
您可能希望发送一些属性,如ID或该行的数据属性(不清楚期望是什么)
好。因此,使用上面的示例可能无法使用Jquery datatables.net库(但适用于常规数据表)。相反,你必须使用render:https://datatables.net/reference/option/columns.render
我在他们的论坛上发布了一个简单的例子:http://live.datatables.net/qemodapi/1/edit
以下是我自己制作的完整代码:
<script type="text/javascript">
$(document).on('click','.delete_btn',function (){
var id = $(this).attr("id").match(/\d+/)[0];
var del_id = $('#example').DataTable().row( id ).data();
var del_id = del_id[0];
console.log(del_id[0]);
$.ajax({
type:'POST',
url:'delete.php',
dataType: 'json', //This says I'm expecting a response that is json encoded.
data: { 'del_id' : del_id},
success: function(data){
if(data['success']){ //You are checking for true/false not yes or no.
console.log('You successfully deleted the row.');
}else{
console.log('The row was not deleted.');
}
}
});
});
</script>