【问题标题】:Why does the AJAX POST request not work为什么 AJAX POST 请求不起作用
【发布时间】:2015-04-07 11:04:07
【问题描述】:
    <script>
        function postComment() {
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5

                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("commentHint").innerHTML = xmlhttp.responseText;
                }
                var comment = document.getElementById("comment").value;
                var id = document.getElementById("postID").value;
                xmlhttp.open("POST", "commentpost.php", true);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp.send("comment=" + comment + "&postID=" + id);
            }
        }

    </script>
    <form>
        <div id="comment">
            <textarea name="comment" id="comment" rows="4" cols="125" style="max-width: 950px; max-height: 140px;" placeholder="<?php echo $_SESSION["name"] ?>, Write Your Comment Here" class="form-control"></textarea><br>
            <div id="commentHint"></div>
            <input type="submit" onclick="postComment()" value="Submit Comment"  class="btn btn-success btn-sm ">

            <input type="hidden" id="postID" name="postID" value="<?php echo $post_id ?>">

        </div>
    </form>

我不知道为什么我的 AJAX POST 请求不起作用... 这是我相应的 PHP 文件中的 POST 变量:
$comment = $_POST["comment"];
$postID = $_POST["postID"];

当我单击提交评论按钮时,它会首先刷新页面并将我带回主页。它也不会触发 php 脚本。我是 AJAX 的新手,有人可以告诉我出了什么问题

【问题讨论】:

    标签: javascript php ajax


    【解决方案1】:

    您的xmlhttp.send(...) 调用在onreadystatechange 处理程序中,要调用处理程序方法,您需要发送请求,这样ajax 方法就不会被执行。

    负责发送请求的代码应该在处理方法之外。

    function postComment() {
        if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
    
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("commentHint").innerHTML = xmlhttp.responseText;
            }
        }//need to close onreadystatechange here
        var comment = document.getElementById("comment").value;
        var id = document.getElementById("postID").value;
        xmlhttp.open("POST", "commentpost.php", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("comment=" + comment + "&postID=" + id);
    }
    

    注意:如果您使用适当的代码缩进,则可以轻松避免此类问题。

    【讨论】:

    • 非常感谢!为什么每次我单击提交时,所有数据都会发送到 url,即使它是一个 POST 请求。我怎样才能防止这种情况发生?
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多