AngularJS - set HTTP header for GET request

Perhaps this is a common problem that may be available on the Internet, but what I got is here.

Adding a custom header to an HTTP request using angular.js

So, I went after him and changed the code to

Header setting

var config = {headers: { 'Authorization': 'XXXYYY token="xxxxxxxx", realm="dash-api"', "X-Testing" : "testing" } }; 

Request for a request:

 return $http.get(api.host+'/agn/12/adv/1860/cam?status=1', config).then(function (response) { return { status:"success", data:response.data.data.active }; }, function (error) { return { status:"error", data:error } }); 

As you can see, the request is executed in methods of the OPTIONS type, and the Authorization token is not set in the request.

Please help me in this matter as I am struggling with two days.

Thank you very much.

enter image description here

+6
source share
2 answers

I think that if you look at the link that you are linking to, you get the same problem as the author and author of this message, stated that this is a CORS problem - the server he was talking to is not CORS support, he confirms this in your comment on your post.

As for why you get an OPTIONS request, this is because CORS makes a pre-flight request to the server to determine whether CORS will be supported before it makes the actual request.

+1
source

You see an OPTIONS request, most likely because you are making a cross-lookup request (CORS). First, your server must support CORS (responding to an OPTIONS request with the Access-Control-Allow-Origin header specified in the response).

If you make a request with a confirmed cross, you need to set withCredentials to true when executing the request.

 $http.get(url, { withCredentials: true }) 
0
source

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


All Articles