Javascript Can';t向服务器发送POST请求

Javascript Can';t向服务器发送POST请求,javascript,java,xmlhttprequest,httpserver,Javascript,Java,Xmlhttprequest,Httpserver,为了便于学习,我正在用Java编写一个基本的线程池web服务器;使用HttpServer和HttpHandler类 server类的run方法如下所示: @Override public void run() { try { executor = Executors.newFixedThreadPool(10); httpServer = HttpServer.create(new InetSocketAddress(por

为了便于学习,我正在用Java编写一个基本的线程池web服务器;使用HttpServer和HttpHandler类

server类的run方法如下所示:

@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) {
        }
    }
StartHandler类实现了HttpHandler,在web浏览器中键入内容时提供html页面。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>

线程池服务器启动
函数btnClicked(){
var http=new XMLHttpRequest();
变量url=”http://localhost:8080//stop";
var params=“abc=def&ghi=jkl”;
http.open(“POST”,url,true);
//随请求一起发送正确的标头信息
http.setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
setRequestHeader(“内容长度”,参数长度);
setRequestHeader(“连接”,“关闭”);
http.onreadystatechange=function(){//在状态更改时调用函数。
如果(http.readyState==4&&http.status==200){
警报(http.responseText);
}
}
http.send(params);
}
停止服务器
基本上,上面的html文件包含一个按钮,当单击该按钮时,应该向URL上的服务器发送POST请求(上面StopHandler的上下文)

StopHandler类还实现了HttpHandler,但是我没有看到在按钮单击时调用StopHandler的handle()函数(我在其中有一个System.out.println,它没有执行)。据我所知,由于上面html页面的按钮单击向设置为StopHandler的上下文发送POST请求,所以不应该执行它的handle()函数吗?当我尝试通过web浏览器执行时,会调用StopHandler的handle()函数


谢谢您的时间。

这更像是一种解决方法,但我能够通过使用表单并绕过XmlHttpRequest正确发送POST请求。尽管我仍然相信XmlHttpRequest应该可以工作

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