title: No data sent from own HTTP server to browser
body: <p>I've created simple Java server that uses HTTP protocol over port 4567 to accept connections and (at least for now) send hardcoded response to browser:</p>
  
  <pre><code>public class Connection extends Thread {
      private boolean stop;
      private Socket socket;
      private BufferedWriter output;
      private BufferedReader input;
  
      public Connection(Socket socket) {
          try {
              this.socket = socket;
              output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
              input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          } catch (IOException ex) {
              System.out.println("Could not create input/output readers: " + ex);
          }
      }
  
      @Override
      public void run() {
          //while (!stop) {
              try {
                  String data = "{'success': 'yes'}".replaceAll("'", "\"");
                  String read;
  
                  // printing request on screen; it's ignored
                  while (!(read = input.readLine()).equals("")) {
                      System.out.println(read);
                  }
  
                  // Last empty line send by browser won't be printed 
                  System.out.println();
  
                  // Writing hardcoded response
                  writeln("HTTP/1.1 200 OK");
                  writeln("Content-Type: application/json; charset=utf-8");
                  writeln("Content-Length: " + data.length());
                  writeln();
                  writeln(data);
                  output.flush();
                  input.close();
                  output.close();
              } catch (IOException ex) {
                  System.out.println("IO Error: " + ex);
              }
          //}
      }    
  
      private void writeln(String... lines) throws IOException {        
          if (lines.length == 0) {
              System.out.println("Writing:");
              output.write("\n");
          }
  
          for (String line : lines) {
              System.out.println("Writing: " + line);
              output.write(line + '\n');
          }
      }
  
      public void setStop(boolean stop) {
          this.stop = stop;
      }
  }
  </code></pre>
  
  <p>Connection instances are created in another thread, it's just <code>new Connection(serversocket.accept()).start();</code></p>
  
  <p>On the browser side, there's AJAX request send by following code:</p>
  
  <pre><code>$(document).ready(function() {
      $.ajax({
          url: "http://192.168.1.212:4567",
          crossDomain: true,
          success: function(data) {
              console.log("Success: " + data);
          },
          error: function (xhr, ajaxOptions, thrownError) {
              console.log("Text: " + xhr.responseText);
              console.log("Status: " + xhr.status);
              console.log("Error: " + thrownError);
        }
      });
  });
  </code></pre>
  
  <p>That's what browser says:</p>
  
  <p><img src="https://i.stack.imgur.com/jE87t.png" alt="enter image description here"></p>
  
  <p>And that's what server says:</p>
  
  <pre><code>GET / HTTP/1.1
  Host: 192.168.1.212:4567
  User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120425 Firefox/10.0.4
  Accept: */*
  Accept-Language: pl,en-us;q=0.7,en;q=0.3
  Accept-Encoding: gzip, deflate
  Connection: keep-alive
  Referer: http://localhost:8383/Client/index.html
  Origin: http://localhost:8383
  
  Writing: HTTP/1.1 200 OK
  Writing: Content-Type: application/json; charset=utf-8
  Writing: Content-Length: 18
  Writing:
  Writing: {"success": "yes"}
  </code></pre>
  
  <p>I know that there's cross-domain request, it's very important part of my project.
  All headers are sent, but there's no data in response.</p>