首页 > 解决方案 > 从文本字段中获取值并使用 AJAX 请求 Javascript 发送它

问题描述

我正在尝试同步客户端和服务器端脚本,客户端从文本框中获取一个值并将其发送到服务器,服务器在服务器上将该输入显示为 cookie。

这是我到目前为止的代码

function loadCookie() {
        //[1] make a new request object
        var xhttp = new XMLHttpRequest();

        //[2] set the request options
        xhttp.open("GET", "index.html", true);

        //[3] define what you will do when you ge a response (callback)
        xhttp.onreadystatechange = function(){
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("input_response").innerHTML = this.responseText;
            }
        };

        //[4] finally send out the request
        xhttp.send();
    }

我有 和 按钮,但我遇到了页面重新加载本身的问题,而不是获取输入的值并将其显示为服务器中的 cookie。我怀疑它与 index.html 的 URL 有关

标签: ajaxserverclient

解决方案


推荐阅读