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