duanaiguang1960 2015-04-07 03:01
浏览 106
已采纳

为什么AJAX POST请求不起作用

    <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>

I have no idea why my AJAX POST request isn't working... Here's the POST vars in my corresponding PHP FILE:
$comment = $_POST["comment"];
$postID = $_POST["postID"];

When ever I click the submit comment button it refreshes the page first and bring me back to the home page. It does not fire the php script either.. I'm new to AJAX can someone please tell me what's wrong

  • 写回答

1条回答 默认 最新

  • dozr13344 2015-04-07 03:04
    关注

    Your xmlhttp.send(...) call is within the onreadystatechange handler, for the handler method to get called you need to send the request so the ajax method is never executed.

    The code that is responsible to send the request should be outside of the handler method.

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

    Note: If you use proper code indentation this kind of issues can be easily avoided.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示