title: JQuery Ajax calls with HTTP Basic Authentication
body: <p>I have a REST based server which I am trying to communicate with using JQuery. Both XML and JSON are available as response formats, so I am using JSON.</p>
  
  <p>The connections are all SSL so HTTP Basic Authentication has been our authorization method of choice, and we have had no problems with other front ends (raw Javascript, Silverlight, etc...)</p>
  
  <p>Now I am attempting to put something together with JQuery and having endless problems using HTTP Basic Authentication.</p>
  
  <p>I have scoured through numerous previous questions most of which either have solutions that do not seem to actually work or blame the entire problem on cross origin access, which I have already overcome in basic Javascript testing. The responses always provide <code>Access-Control-Allow-Origin</code> set to the provided Origin header in the request, and this can be seen in the responses of my tests.</p>
  
  <p>In basic javascript, this entire call is very simply accomplished with:</p>
  
  <pre><code>req.open('GET', 'https://researchdev-vm:8111/card', true, 'test', 'testpassword');
  </code></pre>
  
  <p>The JQuery attempt at this is fairly standard:</p>
  
  <pre><code>        $.ajax({
          username: 'test',
          password: 'testpassword',
          url: 'https://researchdev-vm:8111/card',
          type: 'GET',
          dataType: 'json',
          crossDomain: true,
          /*data: { username: 'test', password: 'testpassword' },*/
          beforeSend: function(xhr){
              xhr.setRequestHeader("Authorization",
                  //"Basic " + encodeBase64(username + ":" + password));
                  "Basic AAAAAAAAAAAAAAAAAAA=");
          },
          sucess: function(result) {
              out('done');
          }
      });
  </code></pre>
  
  <p>The only method that actually seems to work to provide authentication is the direct insertion of the Base64 encoded data in the <code>beforeSend()</code> function. If this is not included, there is no progress made whatsoever. Username and password properties seem to be ignored entirely.</p>
  
  <p>With the <code>beforeSend()</code> behavior provided, the GET call gets a positive response with data included. However, because this is a cross site call, an OPTIONS call is performed ahead of the GET call and always fails, because it does not make use of <code>beforeSend()</code> and therefore gets a 401 response due to failed authentication.</p>
  
  <p>Is there a better way to accomplish what should be a very trivial call? Should the fact that the OPTIONS request does not make use of the <code>beforeSend()</code> function processing be considered a bug? Is there perhaps a way to disable the OPTIONS check entirely? It is not possible to make this call not cross-site, as Javascript seems to consider even a different port number on the same machine as "cross-site".</p>