Jquery 将错误数据传回给.load()
我正在调用一个PHP函数来删除一个文件,使用.load:Jquery 将错误数据传回给.load(),jquery,Jquery,我正在调用一个PHP函数来删除一个文件,使用.load: galleryStatus2$.load('deletePage.php', {pageName : $(e.target).val()}, function(responseTxt,statusTxt,xhr) { if(statusTxt=="success") alert("External co
galleryStatus2$.load('deletePage.php',
{pageName : $(e.target).val()},
function(responseTxt,statusTxt,xhr) {
if(statusTxt=="success")
alert("External content loaded successfully!");
if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
});
/*DeletePage.php*/
<?php
$fileName = $_POST['pageName'] . 'xml'; // comes in without a suffix
$filePath = $_SERVER['DOCUMENT_ROOT'] . "/users/user_" . $_SESSION['user']['id'] . "/xmls/" . $_POST['fileName'];
if(!unlink ($filePath)) {
echo ("<br />delete of $filePath failed");
}
else {
echo ("<br />deletePage.php: delete of $filePath succeeded");
}
?>
在这种情况下,deletePage.php有严重错误。它查找的一个POST值未通过,实际的取消链接操作失败。但是回到客户端,statusTxt报告“成功”和“外部内容加载成功!”
我怎样才能告诉PHP客户端事情进展不顺利
谢谢在您的情况下,您可能需要阅读'responseText'而不是'statusTxt'(不更改'deletePage.php':
galleryStatus2$.load('deletePage.php',
{pageName : $(e.target).val()},
function(responseTxt,statusTxt,xhr) {
if(statusTxt=="success") {
if(responseTxt.indexOf('succeeded') >= 0) {
alert("External content loaded successfully!");
} else {
alert("Error: "+xhr.status+": "+xhr.statusText);
}
} else if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
}
);
第二种选择:
.js
.php
'','msg'=>'';
如果(!取消链接($filePath)){
$response['status']='success';
$response['msg']='
删除$filePath失败';
}否则{
$response['status']='error';
$response['msg']='
deletePage.php:删除$filePath成功';
}
json_编码($response);
?>
您应该阅读的是响应文本,状态文本只是告诉您webservice触发器是否为成功/错误,而不是webservice的响应。因此,deletePage.php的结尾应该类似于:return{“status/error…failed”}?如果您想在将来实现,这是一个更合适的webservice响应。你现在有什么就用什么,我现在什么都没有。也许您可以编辑上面的答案,将我的deletePage.php脚本包括在内,考虑到Web服务出现错误,该脚本经过修改以返回Web服务的正确响应。您可以参考第二个选项
galleryStatus2$.load('deletePage.php',
{pageName : $(e.target).val()},
function(responseTxt,statusTxt,xhr) {
if(statusTxt=="success") {
if(responseTxt) {
var obj = jQuery.parseJSON(responseTxt);
if(obj && obj.status == 'success') {
//success do whatever you need to do
alert(obj.msg);
} else {
//fail do whatever you need to do
alert(obj.msg);
}
}
} else if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
}
);
<?php
$fileName = $_POST['pageName'] . 'xml'; // comes in without a suffix
$filePath = $_SERVER['DOCUMENT_ROOT'] . "/users/user_" . $_SESSION['user']['id'] . "/xmls/" . $_POST['fileName'];
$response = array('status' => '', 'msg' => '');
if(!unlink ($filePath)) {
$response['status'] = 'success';
$response['msg'] = '<br />delete of $filePath failed';
} else {
$response['status'] = 'error';
$response['msg'] = '<br />deletePage.php: delete of $filePath succeeded';
}
json_encode($response);
?>