AJAX上的403和404错误,代码在其他地方有效

时间:2017-09-06 14:17:38

标签: javascript php ajax

所以我不知道这个代码的问题是什么...因为我让它在我创建的另一个网站上工作。这个想法很简单。在一个按钮上添加一个事件监听器,然后当按下它时,系统会询问用户是否确实要删除该行,然后AJAX将该数据发送到我的服务器,这将完成其余的工作。但是,当我按下删除时,我收到了403错误更奇怪的东西,在网址栏中,我看到的是HTML 4.01页面,它说在URL栏中的以下内容:

The link on the
<a href="http://localhost/afton/index.php%3faction=home">referring
page</a> seems to be wrong or outdated. Please inform the author of
<a href="http://localhost/afton/index.php%3faction=home">that page</a>

即使如果你把它放到浏览器栏中,它就可以了。这是我的AJAX / JavaScript代码:

var delBut = table.getElementsByTagName("button");
for (var i = 0; i < delBut.length; i++) {
    var but = delBut[i];
    but.addEventListener("click", function() {
        var really = confirm("Are you sure you want to delete this entry? This cannot be undone.");
        if (really) {
            var table = but.parentElement.parentElement.parentElement.parentElement.getElementsByTagName("input").location.value.charAt(0);
            switch(table){
                case 'h': table = "logins"; break;
                case 'n': table = "network"; break;
                case 'p': table = "passwords"; break;
            }
        var ID = but.parentElement.parentElement.id
        var loc = but.parentElement.parentElement.parentElement.parentElement.getElementsByTagName("input").location.value;
        console.log(table, ID, loc);
        var ajax = new XMLHttpRequest();
        ajax.open("post", "../internalPages/index.php?action=deleteEntry", true);
        ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajax.send('table=' + encodeURIComponent(table) + '&ID=' + encodeURIComponent(ID) + '&loc=' + encodeURIComponent(loc));
        ajax.onload = function() {
            window.location = ajax.responseText.trim();
        }
    }
    }, false);
}

顺便说一下,我console.log编辑了变量,它们都被正确捕获。这是PHP代码:

case "deleteEntry":
    $location = filter_input(INPUT_POST, "loc", FILTER_SANITIZE_STRING);
    $ID = filter_input(INPUT_POST, "ID", FILTER_SANITIZE_NUMBER_INT);
    $table = filter_input(INPUT_POST, "table", FILTER_SANITIZE_STRING);
    if (empty($ID) || empty($table)){
        $message = "<p class='badMessage'>There was an application error. Please try again.</p>";
        $_SESSION['message'] = $message;
        if ($location !== "home"){
            echo "/afton/internalPages/index.php?action=$location";
        } else {
            echo "/afton/index.php?action=$location";
        }
        exit;
    } switch($table) {
        case "passwords": $table = "passwords"; break;
        case "network": $table = "network"; break;
        case "logins": $table = "logins"; break;
        default:
            $message = "<p class='badMessage'>There was an application error. Please refresh the page and try again.</p>";
            $_SESSION['message'] = $message;
        if ($location !== "home"){
            echo "/afton/internalPages/index.php?action=$location";
        } else {
            echo "/afton/index.php?action=$location";
        }
        exit;
        break;
    }
    $result = deleteEntry($ID, $table);
    if ($result === 1) {
        $message = "<p class='goodMessage'>The entry was successfully deleted from the database</p>";
        $_SESSION['message'] = $message;
        if ($location !== "home"){
            echo "/afton/internalPages/index.php?action=$location";
        } else {
            echo "/afton/index.php?action=$location";
        }
        exit;
    } else {
        $message = "<p class='badMessage'>Nothing was deleted from the database</p>";
        $_SESSION['message'] = $message;
        if ($location !== "home"){
            echo "/afton/internalPages/index.php?action=$location";
        } else {
            echo "/afton/index.php?action = $location";
        }
        exit;            
    }
break;

2 个答案:

答案 0 :(得分:0)

编辑:似乎我猜错了...根据更新的评论。

查看您的PHP,您有类似echo "/afton/internalPages/index.php?action=$location";}echo "/afton/index.php?action=$location";的内容,这使我认为这些页面是在http://localhost/afton/

提供的

问题是,在您的ajax上,您试图访问../internalPages/index.php?action=deleteEntry ../会使浏览器向上移动一个目录级别,因此如果页面在http://localhost/afton/提供,它将会翻译为http://localhost/internalPages/index.php?action=deleteEntry

您有2个选项,只需删除../或继续使用PHP中的绝对网址,即/afton/internalPages/index.php?action=deleteEntry

答案 1 :(得分:0)

好吧,如果有人需要一个好的羞辱.......这是我。我的服务器上的目录“internalPages”拼写为“interalPages”。你没事,随意使用你想要的所有我告诉你的sos。