在javascript中获取 http.post,然后发送响应

Get http.post in javascript then send a response

本文关键字:然后 响应 post javascript 获取 http      更新时间:2023-09-26

在php中有很多方法可以做到这一点,但我不是服务器端,所以有没有办法在纯JS中做到这一点?

Pure JS?是的,但仅限于服务器(具体细节取决于您使用的SSJS实现,Express + NodeJS目前很流行)。

您无法在客户端上接收 POST 请求。客户端发出请求。服务器做出响应。这就是HTTP的工作方式。

<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>

查看更多

相关文章: