0

我正在使用 .load 调用 PHP 函数来删除文件:

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);
            });

/* 删除页面.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 告诉客户事情进展不顺利。

谢谢

4

1 回答 1

1

在您的情况下,您可能需要阅读“responseTxt”而不是“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

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

<?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);
?>
于 2013-08-29T03:23:47.503 回答