使用 HTML 提交按钮发送简单的 HTTP 请求

Ari*_*ana 5 html javascript dom http

index.html在服务器端的页面上添加了一个提交按钮

<form method="get" action="/start">
    <h5>Start Ad-Placer!</h5>
    <input type="submit" value="Start">
</form>
Run Code Online (Sandbox Code Playgroud)

我需要按钮来简单地发送一个http请求http://127.0.0.1:8484/get/start来启动某个进程。然后,一旦该过程在几秒钟后完成,我需要简单地显示一条带有响应消息的警报消息,确认它已完成。

我怎样才能以最小的努力做到这一点(不使用 JQuery 或其他库)。

the*_*epi 4

如果您尝试类似的操作,您可以发送 HTTP 请求,然后发出响应警报。只需将https://google.com更改为您的网址即可。

<h5>Start Ad-Placer!</h5>
<input type="submit" value="Start" onclick="submit()">

<script type="text/javascript">
    function submit() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                alert(xhr.response);
            }
        }
        xhr.open('get', 'https://google.com', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xhr.send();
    }

</script>
Run Code Online (Sandbox Code Playgroud)