使用HTML提交按钮发送简单的HTTP请求 [英] Send simple HTTP request with HTML submit button

查看:105
本文介绍了使用HTML提交按钮发送简单的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have added a submit button to the index.html page that resides on my server side

<form method="get" action="/start">
    <h5>Start Ad-Placer!</h5>
    <input type="submit" value="Start">
</form>

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

I need the button to simply send a http request to http://127.0.0.1:8484/get/start to start some process. Then once the process is done after a few seconds, I need to simply display an alert message with the response message, acknowledging it is done.

如何以最少的努力做到这一点(无需使用JQuery或其他库).

How can I do that with minimum efforts (without using JQuery or other libraries).

推荐答案

如果尝试执行类似的操作,则可以发送HTTP请求,然后发出警报.只需将 https://google.com 更改为您的URL.

If you try something like this you can send a HTTP request and then alert a response. Just change https://google.com to your URL.

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

这篇关于使用HTML提交按钮发送简单的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆