title: How to send POST request from browser?
body: <p>I'm trying to make a server in java that can handle requests sent from the browser. My problem is that I can't get the browser to actually send the request, all it sends is an OPTIONS request. Here's the error in the browser:
  <a href="https://i.stack.imgur.com/pVaFg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pVaFg.png" alt="enter image description here" /></a></p>
  <p>It isn't an actual website that I'm using, it's just an html page that would be opened by the java application. Here's the html:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;body&gt;
      &lt;button type="button" onclick="sendRequest()"&gt;Click Me!&lt;/button&gt;
      &lt;script&gt;
          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
              }));
          }
      &lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>And here's the java server that listens for the requests:</p>
  <pre><code>public class ServerTest
  {
      public static void main(String[] args) throws Exception
      {
          HttpServer server = HttpServer.create(new InetSocketAddress(9000), 0);
          server.createContext(&quot;/test&quot;, new MyHandler());
          server.setExecutor(null);
          server.start();
      }
  
      static class MyHandler implements HttpHandler
      {
          public void handle(HttpExchange exchange) throws IOException
          {
              System.out.println(&quot;Handling request: &quot; + exchange.getRequestMethod());
  
              if (exchange.getRequestMethod().equalsIgnoreCase(&quot;POST&quot;) ||         
              exchange.getRequestMethod().equalsIgnoreCase(&quot;OPTIONS&quot;))
              {
                  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 = &quot;OK&quot;;
                      exchange.sendResponseHeaders(200, response.length());
                      OutputStream os = exchange.getResponseBody();
                      os.write(response.getBytes());
                      os.close();
                      exchange.close();
                  } catch (NumberFormatException e)
                  {
                      e.printStackTrace();
                  }
              }
          }
      }
  }
  </code></pre>
  <p>The server works fine, but it only receives the OPTIONS request. How can I make the html page send the actual POST request?</p>