How to assign XMLHTTPREQUEST primary authentication header?

I read a lot of preflight and CORS answers, so please do not post links that link to what I should read. Many of the answers relate to the server perspective, but I am the client in this case. Set source header? My assumption is that this is a simple request, am I right?

 req.open("POST", url, true);
 req.setRequestHeader( 'Content-Type',   'application/blahblah' );
 req.setRequestHeader( 'Accept', 'application/blahblah' );
 req.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass)); 
 req.send();

But it still does not work, my mistake is:

The response to the preflight check request does not pass the access control check. There is no "Access-Control-Allow-Origin" header on the requested resource. Therefore, the original 'null' is not allowed. The response was an HTTP status code of 500.

+4
source share
4

:

req.setRequestHeader('Authorization', 'Basic [base64 encoded password here]' );
+5

req.setRequestHeader('Authorization','Basic ' + Base64StringOfUserColonPassword);
+8

, - , .

. , , CORS.

Origin . . , , , file:/// http://.

+4

API, , XMLHttpRequest OPTIONS, API.

, :

var invocation = new XMLHttpRequest();
invocation.open("GET", url, true, username, password);
invocation.withCredentials = true;

That will add an authorization header, as well as avoid a preliminary check request.

+2
source

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


All Articles