简体   繁体   English

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

[英]Get http.post in javascript then send a response

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

Pure JS?纯JS? Yes, but only on the server (the specifics would depend on which SSJS implementation you use, Express + NodeJS is popular at present).是的,但仅限于服务器(具体取决于您使用的 SSJS 实现,Express + NodeJS 目前很流行)。

You can't receive a POST request on the client.您无法在客户端上接收 POST 请求。 The client makes the request.客户端发出请求。 The server makes the response.服务器做出响应。 That is how HTTP works.这就是 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>

See more查看更多

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

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