title: Cross-domain GET request not working in jQuery, with a C# backend server
body: <p>I have a separate HTTP server hosted on another domain that I use as my backend server (C#). It creates a TcpListener, and on connection creates a new thread to deal with the connection.</p>
  
  <p>I use jQuery to make an ajax get request to this server, and jQuery sends an OPTIONS request instead. After some research, I added this segment into my program in hopes that it would continue with the HTTP request afterwards. (ns is an instance of NetworkStream, from TcpClient.GetStream())</p>
  
  <pre><code>if (req1.StartsWith("OPTIONS"))
              {
                  //req1 = req1.Split('\n')[0].Substring(9);
                  string headers = string.Format(
                      "HTTP/1.0 200 OK\r\n"+
                 "Access-Control-Allow-Origin: *\r\n"+
                 "Access-Control-Allow-Methods: *\r\n"+
                 "Access-Control-Max-Age: 1728000\r\n"+
                 "Access-Control-Allow-Headers: *\r\n"+
                 "Connection: Keep-Alive\r\n"+
                 "Content-Type: text/plain\r\n\r\n");
                  ns.Write(Encoding.ASCII.GetBytes(headers), 0, Encoding.ASCII.GetBytes(headers).Length);
                  ns.Close(); client.Close();  return;
              }
  </code></pre>
  
  <p>Unfortunately, the browser keeps sending OPTIONS requests to the server, even though the server sends back Access-Control headers that should let it have unlimited access.</p>
  
  <p>Here are the network logs for 1 of these requests: <a href="http://pastebin.com/T07Whvem" rel="nofollow">http://pastebin.com/T07Whvem</a></p>
  
  <p>Any suggestions to make this work? The javascript code is pretty trivial, but I'll include it:</p>
  
  <pre><code>//this is just a long-poll
  function waitForMsg(){
  
      $.ajax({
          url: "http://127.0.0.1:6969/2|||ub",
          dataType: "text",
  
         async: true, 
         cache: true,
          timeout:50000, 
  
          success: function(data){ 
              addmsg("new", data); 
              setTimeout(
                  'waitForMsg()', 
                  1000 
              );
          },
          error: function(XMLHttpRequest, textStatus, errorThrown){
              addmsg("error", textStatus + " (" + errorThrown + ")");
              setTimeout(
                  'waitForMsg()', 
                  "1000"); 
          },
      });
  };
  
  $(document).ready(function(){
      waitForMsg(); /* Start the inital request */
  });
  </code></pre>
  
  <p>I can't use an existing web-server because this is not really a standard webserver; merely a server that can take instructions via http. </p>
  
  <p>Thanks!</p>