【问题标题】:How to send POST request from browser? [duplicate]如何从浏览器发送 POST 请求? [复制]
【发布时间】:2022-01-06 08:51:18
【问题描述】:

我正在尝试在 java 中创建一个可以处理从浏览器发送的请求的服务器。我的问题是我无法让浏览器实际发送请求,它发送的只是一个 OPTIONS 请求。这是浏览器中的错误:

这不是我正在使用的实际网站,它只是一个可由 java 应用程序打开的 html 页面。这是html:

<!DOCTYPE html>
<html>

<body>
    <button type="button" onclick="sendRequest()">Click Me!</button>
    <script>
        function sendRequest() {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "http://172.26.48.1:9000/test", true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.send(JSON.stringify({
                "time": 16400
            }));
        }
    </script>
</body>

</html>

这是侦听请求的 java 服务器:

public class ServerTest
{
    public static void main(String[] args) throws Exception
    {
        HttpServer server = HttpServer.create(new InetSocketAddress(9000), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null);
        server.start();
    }

    static class MyHandler implements HttpHandler
    {
        public void handle(HttpExchange exchange) throws IOException
        {
            System.out.println("Handling request: " + exchange.getRequestMethod());

            if (exchange.getRequestMethod().equalsIgnoreCase("POST") ||         
            exchange.getRequestMethod().equalsIgnoreCase("OPTIONS"))
            {
                try
                {
                    InputStream is = exchange.getRequestBody();
                    StringBuilder sb = new StringBuilder();
                    for (int ch; (ch = is.read()) != -1;)
                        sb.append((char) ch);

                    System.out.println(sb.toString());

                    String response = "OK";
                    exchange.sendResponseHeaders(200, response.length());
                    OutputStream os = exchange.getResponseBody();
                    os.write(response.getBytes());
                    os.close();
                    exchange.close();
                } catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务器工作正常,但它只接收 OPTIONS 请求。如何让 html 页面发送实际的 POST 请求?

【问题讨论】:

  • 您做得很好,但请求被 CORS 策略阻止
  • 使用Fetch api - 更简单,更可配置
  • 不,因为我没有使用 Postman。我正在尝试只使用 java、html 和 javascript。
  • 没关系,讲区别和js为什么会报错。

标签: javascript java html


【解决方案1】:
function sendRequest() {
   var xhttp = new XMLHttpRequest();
   xhttp.open("POST", "http://172.26.48.1:9000/test", true); 
   xhttp.setRequestHeader("Content-Type", "application/json");
   xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        // Response
        var response = this.responseText;
      }
   };
   xhttp..send(JSON.stringify({
                "time": 16400
            }));
}

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 2011-06-15
    • 2015-04-26
    • 1970-01-01
    • 2012-05-19
    • 2017-12-06
    • 2016-09-14
    • 2011-10-28
    相关资源
    最近更新 更多