我遇到的问题....我在php中有监听器脚本执行此操作:
if ($count != 1) {echo 'no';} else { echo "yes";}
因此,如果任务成功与否,它会回应“是”或“否” 在我的页面上我有这个:
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总是处理这个,好像响应是“是”!我错过了一些明显的东西吗?
答案 0 :(得分:2)
为您节省很多麻烦,并使用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断点以帮助调试。
答案 1 :(得分:1)
可能你的回复中有一些空白。请尝试使用此代码:
if ($.trim(data) == 'no') {