繁体   English   中英

使用PHP的AJAX请求

[英]AJAX request using PHP

我想在单击按钮时触发AJAX请求,但是我无法在后端触发它。

index.php

<html>
    <head>
        <script type="text/javascript">
            var req = new XMLHttpRequest();
            function send1()
            {
                req.open("GET", "process.php?q=hello", true);
                req.send();         
                alert(req.responseText);      
            }
        </script>
    </head>    

    <button onclick=send1()>send</button>

</html>

process.php

<?php
$new= $_GET['q'];
echo $new;
?>

这应该让我在警报框中“打招呼”,为什么不是呢?

AJAX中的第一个A表示“异步”。 您需要做的就是侦听readyState的更改:

req.open(...);
req.onreadystatechange = function() {
    if( this.readyState == 4) {
        if( this.status == 200) alert(this.responseText);
        else alert("HTTP error "+this.status+" "+this.statusText);
    }
};
req.send();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM