简体   繁体   English

无法将POST请求发送到服务器

[英]Can't send POST request to server

I'm writing a basic thread pooled web server in Java for learning purposes; 我正在用Java编写一个基本的线程池Web服务器,以供学习之用。 using the HttpServer and HttpHandler classes. 使用HttpServer和HttpHandler类。

The server class has its run method like so: 服务器类具有以下运行方法:

@Override
    public void run() {
        try {
            executor = Executors.newFixedThreadPool(10);
            httpServer = HttpServer.create(new InetSocketAddress(port), 0); 
            httpServer.createContext("/start", new StartHandler());
            httpServer.createContext("/stop", new StopHandler());
            httpServer.setExecutor(executor);
            httpServer.start();
        } catch (Throwable t) {
        }
    }

The StartHandler class, which implements HttpHandler, serves up an html page when typing http://localhost:8080/start in a web browser. 实现HttpHandler的StartHandler类在Web浏览器中键入http:// localhost:8080 / start时会提供一个html页面。 The html page is: html页面是:

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Thread Pooled Server Start</title>
    <script type="text/javascript">
        function btnClicked() {
            var http = new XMLHttpRequest();
            var url = "http://localhost:8080//stop";
            var params = "abc=def&ghi=jkl";
            http.open("POST", url, true);

            //Send the proper header information along with the request
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            http.setRequestHeader("Content-length", params.length);
            http.setRequestHeader("Connection", "close");

            http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4 && http.status == 200) {
                    alert(http.responseText);
                }
            }
            http.send(params);
        }
    </script>
</head>
<body>
    <button type="button" onclick="btnClicked()">Stop Server</button>
</body>
</html>

Basically, above html file contains a single button, which when clicked is supposed to send a POST request to the server on the URL http://localhost:8080/stop (the context for StopHandler above). 基本上,上面的html文件包含一个按钮,单击该按钮应该会向URL http:// localhost:8080 / stop (上述StopHandler的上下文)上的服务器发送POST请求。

The StopHandler class also implements HttpHandler, but I don't see the StopHandler's handle() function being called at all on the button click (I've a System.out.println in it which isn't executed). StopHandler类也实现了HttpHandler,但是单击按钮时根本看不到StopHandler的handle()函数(我有一个System.out.println,它没有执行)。 From what I understand, since the above html page's button click sends a POST request to the context http://localhost:8080/stop which is set to StopHandler, shouldn't it's handle() function be executed? 据我了解,由于上述html页面的按钮单击将POST请求发送到设置为StopHandler的上下文http:// localhost:8080 / stop ,因此是否应该执行handle()函数? When I tried executing http://localhost:8080/stop via a web browser, the StopHandler's handle() function is called. 当我尝试通过Web浏览器执行http:// localhost:8080 / stop时 ,将调用StopHandler的handle()函数。

Thanks for your time. 谢谢你的时间。

It's more of a workaround, but I was able to send POST request correctly by using a form and bypassing XmlHttpRequest. 这更多是一种解决方法,但是我可以通过使用表单并绕过XmlHttpRequest来正确发送POST请求。 Although I still believe XmlHttpRequest should work. 尽管我仍然相信XmlHttpRequest应该可以工作。

<form action="http://localhost:8080/stop" method="post">
        <input type="submit" value="Stop Server">
</form>

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

相关问题 无法发送异步发布请求 - Can't send async post request NodeJs:无法发送带有正文参数的 POST 请求:在服务器上时未定义 req.body.param - NodeJs: Can't send POST request with body parameters: undefined req.body.param when on server 编辑:无法使用 Fetch API 向 Express 服务器发送 POST 请求,不断收到 JSON.parse 错误 - EDITED: Can't send POST request to Express server with Fetch API, keep getting JSON.parse error chrome 发送帖子请求无法获取请求正文 - chrome send post request can't get request body 为什么这个POST请求不会向服务器发送JSON字符串? - Why won't this POST request send a JSON string to the server? 无法在Ionic 3 / Angular 5中发送HTTP POST请求 - Can't send an HTTP POST request in Ionic 3 / Angular 5 无法从 Chrome 扩展程序向 REST API 发送 POST 请求 - Can't send POST request to a REST API from a Chrome extension 无法通过发布请求将授权令牌发送到Flask API - Can't send authorization token to flask api with post request 我无法通过POST方法将请求从angularjs发送到php - I can't send a request by POST method from angularjs to php AJAX发送发布请求,不能将值返回给其他函数吗? - AJAX send post request,can't return the value to other function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM