Question

I am trying to reach some RESTful services that are running under Apache preemtive basic authentication. I am using jquery Ajax, and sending the user and password with the 'Authentication' header. However, my request is throwing an empty error every time it runs.

Here is the full $.ajax call:

$.ajax({
      cache:false,
      url: urladdress,
      type:'GET',
      async:false,
      headers: { "cache-control": "no-cache" },
      dataType: "html", //or xml or json
      contentType: "html",
       beforeSend : function(req) 
       {
          req.setRequestHeader('Authorization', "Basic " +  base64string); //user:password);
       },
      success: function(data){
         successcallback(data);
      },
      error: function(xhRequest, ErrorText, thrownError){
           alert("ERROR: "+ JSON.stringify(xhRequest));
      },
      complete: function(result){
        ...
      }
});

What I am doing wrong? Is there something that I am ignoring here? Thanks.

Was it helpful?

Solution

The solution is on the server-side, you must include some custom headers in response to cross domain works. Experiment send the following headers in response:

Access-Control-Allow-Origin: yourApiDomain

Access-Control-Allow-Methods: *

  • In case of using custom headers:

    Access-Control-Expose-Headers: X-My-Custom-Header, X-Another-Custom-Header

    Access-Control-Allow-Headers: X-My-Custom-Header, X-Another-Custom-Header

  • In case of using HTTP Cookies and HTTP Authentication information:

    Access-Control-Allow-Credentials: true

For more informations read the documentation in MDN about CORS:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top