title: Ajax crossdomain request with jquery and BASIC-authentication?
body: <p>I really appreciated if somebody could tell me what is wrong in that authentication? I take the authentication off from the server to test without it, but javascript is broken.</p>
  
  <pre><code> $('#btnSignIn').click(function() {
              var username = $("#username").val();
              var password = $("#password").val();
  
      function make_base_auth(user, password) {
              var tok = user + ':' + password;
  
              var final = "Basic " + $.base64.encode(tok);
              console.log("FINAL----&gt;" +final);
              alert("FINAL----&gt;" +final);
  
              return final;
  }
  
              $.ajax({
                          type: "GET",
                          contentType: "application/json",
                          url: "http://localhost:8080/SesameService/webresources/users/secured/login",
                          crossDomain: true,
                          dataType: "text",
                          async: false,
                          data: {},
                          beforeSend: function (xhr){ 
                              xhr.setRequestHeader('authorization', make_base_auth(username, password));
                         },
                          success: function() {
                              alert('Thanks for your signin in! ');
  
                          },
                          error: function(jqXHR, textStatus, errorThrown) {
                              console.log(textStatus, errorThrown);
                              alert(' Error in signIn-process!! ' +textStatus);
                          }
                      });
          });
  </code></pre>
  
  <p>ERROR: If I take the </p>
  
  <pre><code>beforeSend: function (xhr){ 
      xhr.setRequestHeader('authorization', make_base_auth(username, password));
  </code></pre>
  
  <p>-part off from the function, I can get into REST-service. I haven't got authentication on at the moment. Could this be the reason or shoud I have the authentication on in the server when using that header?</p>
  
  <pre><code>contentType: "application/json",
  
  @GET
  @Path("/secured/login")
  @Produces({"text/plain"})
  public String login() {
  
      return "Is it working or not?";
  }
  </code></pre>
  
  <hr>
  
  <p>When using beforeSend-part in JS, I got an error:</p>
  
  <pre><code>[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://localhost:8383/Sesame/assets/js/jquery.min.js :: .send :: line 3" data: no] { message="Failure"
  , result=2147500037
  , name="NS_ERROR_FAILURE"
  , 
  </code></pre>
  
  <p>If I have understood correctly, "authorization + Basic " in header tells the Glassfish-server, that basic-authentication will be done. After authentication it goes to REST-service and in my case returns json-object to HTML5-client. HTML5-client is running in localhost:8383 and the rest services are running in localhost:8080.</p>
  
  <p>If I run secured rest-services straight in localhost:8080, it is working, so that is not the problem. The problem is that when I am using or trying to use rest-services from different domain localhost:8383, I get get the JS-error  console.log('----ERROR IN ------Siging IN----------'); I am not 100 % sure, but I think that the problem is 401 unauthorized, so cross domain authentication is not working.</p>
  
  <p>Shoud I insert crossDomain: true? I have seen it somewhere, maybe that can be the case?</p>
  
  <p>In serverside I've got the filter:</p>
  
  <pre><code>&lt;init-param&gt;
          &lt;param-name&gt;com.sun.jersey.spi.container.ContainerResponseFilters&lt;/param-name&gt;
          &lt;param-value&gt;util.CrossOriginResourceSharingFilter&lt;/param-value&gt;
      &lt;/init-param&gt;
  
  public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
      response.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
      response.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
      response.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "content-type");
      return response;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>QUESTIONS</strong></p>
  
  <p>1) is it possbile that if server is not configured for authentication and I still use the authorization-header breaks the app and cause the error?</p>
  
  <p>2) What that JS-error means?</p>
  
  <p>Cheers,
  Sami</p>