从AJAX / PHP获得响应

时间:2017-09-02 01:34:24

标签: javascript php ajax

虽然我对AJAX不熟悉,但我真的不知道为什么这不能正常工作。我有一个通过click事件监视器可编辑的表,然后有另一个事件监听器监听enter键的按键,当发生这种情况时获得不同的变量并向服务器发送一个AJAX发布请求。但是,当我按下回车键时,没有任何反应。没有任何反应,我的意思是我留在我所在的页面而不是服务器上的PHP函数指示我在别处。奇怪的是,在我ajax.send(vars)之后,我控制台。忘记了我的ajax对象并且响应显示正在提供HTML页面,但是:
1.它显示了我的ID,loc或列值为空时会得到的响应(当我记录这些变量时,它们不是) 那个页面没有送达。 ajax对象显示了响应,但我(最终用户)没有看到响应 这是我的JavaScript:

        index.addEventListener("keypress", function(e){
        var key = e.keyCode;
            if (key === 13) {
                var entry = e.path[0].innerText;
                var tabID = e.path[1].id;
                var table = tabID.charAt(0);
                if (table === 'o') {
                    table = "otheraccounts";
                } if (table === 'wh') {
                    table = "wheatstoneaccounts";
                } if (table === 'wo') {
                    table = "wideorbitaccounts";
                }
                var ID = tabID.charAt(1);
                var column = e.path[0].id;
                var loc = e.path[3].getElementsByTagName("input").location.value;
                var ajax = new XMLHttpRequest();
                ajax.open("post", "../other/index.php?action=updateTable", true);
                ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                ajax.send(entry, table, ID, column, loc);
            }
    }, false)

这是PHP

case 'updateTable':
    $column = filter_input(INPUT_POST, 'column', FILTER_SANITIZE_STRING);
    $ID = filter_input(INPUT_POST, 'ID', FILTER_SANITIZE_NUMBER_INT);
    $table = filter_input(INPUT_POST, 'table', FILTER_SANITIZE_STRING);
    $entry = filter_input(INPUT_POST, 'entry', FILTER_SANITIZE_STRING);
    $location = filter_input(INPUT_POST, 'loc', FILTER_SANITIZE_STRING);
    if (empty($ID) || empty($column) || empty($table)){
        $message = "<p class='message'>Stop tampering with my IDs!!</p>";
        $_SESSION['message'] = $message;
        header("Location: /radiosite/other/index.php?action=$location");
        exit;
    }
    $result = updateEntry($column, $ID, $table, $entry);
    if ($result === 1) {
        $message = "<p class='message'>The entry was successfully updated in the database</p>";
        $_SESSION['message'] = $message;
        header("Location: /radiosite/other/index.php?action=$location");
        exit;
    } else {
        $message = "<p class='message'>Nothing was added to the database</p>";
        $_SESSION['message'] = $message;
        header("Location: /radiosite/other/index.php?action=$location");    
        exit;            
    }
break;

1 个答案:

答案 0 :(得分:1)

AJAX在获取重定向标头时不会重定向浏览器。这只是告诉它转到备用URL以获取响应数据。

更改PHP,以便将新URL作为字符串返回。改变如下行:

header("Location: /radiosite/other/index.php?action=$location");

为:

echo "/radiosite/other/index.php?action=$location";

然后在Javascript代码中,您可以阅读响应并将其分配给window.location以执行重定向。

您还没有正确发送POST参数。您需要构造URL编码的字符串。

var ajax = new XMLHttpRequest();
ajax.open("post", "../other/index.php?action=updateTable", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send('entry=' + encodeURIComponent(entry) + '&table=' + encodeURIComponent(table) + '&ID=' + encodeURIComponent(ID) + '&column=' + encodeURIComponent(column) + '&loc=' + encodeURIComponent(loc));
ajax.onload = function() {
    window.location = ajax.responseText.trim();
};