Jquery ajax & preemptive basic auth

I am trying to access some RESTful services that run under basic Apache preemtive authentication. I use jquery Ajax and send the user and password with the header "Authentication". However, my request triggers an empty error every time.

Here is the full call to $ .ajax:

$.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 am I doing wrong? Is there something that I ignore here? Thanks.

+6
source share
1 answer

The solution is on the server side, you must include some custom headers in response to working with cross domains. The experiment will 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 the case of using HTTP cookies and HTTP authentication:

    Access-Control-Allow-Credentials: true

For more information, read the MDS CORS documentation:

+1
source

Source: https://habr.com/ru/post/943465/


All Articles