gpt4 book ai didi

来自帖子的 Jquery 响应

转载 作者:行者123 更新时间:2023-12-01 01:17:00 31 4
gpt4 key购买 nike

我有问题......我在 php 中有一个监听器脚本可以执行以下操作:

  if ($count != 1) {echo 'no';} else { echo "yes";}

因此它会回显"is"或“否”,具体取决于任务是否成功在我的页面上有这个:

   jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function (r) {
if (r) $.post('includes/publishers/delete-publisher.php?publisherid=' + publisherid,
function(data) {
if (data == 'no') {
$.jGrowl('Error - PUBLISHER WAS ALREDY DELETED !');
alert("Data Loaded: " + data);
} else {
$(element).parents('tr').remove();
$.jGrowl('Publisher deleted');
alert("Data Loaded: " + data);
}
});
});

问题是尽管delete-publisher.php回显“否”(我看到它在警报框中回显)-JQuery总是处理这个,就好像响应是"is"!?我是否遗漏了一些明显的东西?

最佳答案

使用 JSON 将响应发送回 JavaScript,可以省去很多麻烦。做这样的事情(未经测试):

PHP 端:

header('Content-type: application/json');
if ($count != 1) {
echo json_encode(array('success'=>false));
} else {
echo json_encode(array('success'=>true))
}

JS 端:

jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function (r) {
if (r) $.post('includes/publishers/delete-publisher.php?publisherid=' + publisherid,
function(data) {
if (data.success == false) {
$.jGrowl('Error - PUBLISHER WAS ALREDY DELETED !');
alert("Data Loaded: " + data);
} else {
$(element).parents('tr').remove();
$.jGrowl('Publisher deleted');
alert("Data Loaded: " + data);
}
}, 'json');

});

这样你也可以将错误消息传递回 JS。

您还应该习惯使用Firebug for Firefox之类的工具,这样您就可以查看网络流量的详细信息,并发布参数、响应头等,设置JS断点以帮助调试。

关于来自帖子的 Jquery 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12536996/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com