[英]Why is the Ajax call not executing?
我有一个表,用户可以删除每条记录:
<td style='text-align: center;'>
<button class='btn btn-danger btn-xs delete' id=9 data-title='Delete' data-toggle='modal' data-target='#delete' >
<span class='glyphicon glyphicon-trash'></span>
</button>
</td>
jQuery
/ AJAX
调用是:
$('.delete').click(function(e) {
var id = $(this).attr('id');
e.preventDefault();
alert(id);
bootbox.confirm("Are you sure? ", function(r) {
if (r) { // Sent request to delete order with given id
alert('SENT REQUEST');
$.ajax({
type: 'GET',
url: 'delete.php',
data: 'id='+id,
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
}
});
document.location.reload();
}
});
});
而delete.php是
if($_GET['id']) {
$id = $_GET['id'];
$sql_delete = "DELETE FROM debiteuren WHERE id = " . $id;
mysqli_query($link,$sql_delete) or die(mysqli_error($link));
}
知道为什么它不起作用吗?
我得到警报ID和模式“你确定吗?” 和警报“发送请求”。 但在此之后,页面重新加载和ID = 9的记录尚未删除。
亲切的问候,,
阿里
你的ajax执行异步尝试在ajax完成后重新加载页面:
$('.delete').click(function(e) {
var id = $(this).attr('id');
e.preventDefault();
alert(id);
bootbox.confirm("Are you sure? ", function(r) {
if (r) { // Sent request to delete order with given id
alert('SENT REQUEST');
$.ajax({
type: 'GET',
url: 'delete.php',
data: {id:id},
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
document.location.reload();
}
});
}
});
并且正如Rayon Dabre建议从php页面返回一些内容
注意使用data-id="9"
表示有效的html
我觉得你正在reload()
在错误的地方,应该在success
回调中移动。
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
document.location.reload(); //<-----move it here.
}
而且我猜你每次都会收到NO
警报,因为你没有从服务器发回适当的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.