javascript - AngularJS - set HTTP header for GET request - Stack Overflow

admin2025-04-19  0

May be this is a general issue, which can be available on internet, But what I got is here.

Adding a custom header to HTTP request using angular.js

So I followed the same, and changed the code to

Setting header

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

The get request call:

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 are going in method type OPTIONS, and the Authorization token is not set in the request.

Please help me in this issue, as I am struggling from two days.

Thanks a lot.

May be this is a general issue, which can be available on internet, But what I got is here.

Adding a custom header to HTTP request using angular.js

So I followed the same, and changed the code to

Setting header

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

The get request call:

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 are going in method type OPTIONS, and the Authorization token is not set in the request.

Please help me in this issue, as I am struggling from two days.

Thanks a lot.

Share edited May 23, 2017 at 10:32 CommunityBot 11 silver badge asked Jul 3, 2014 at 1:58 RONERONE 5,4859 gold badges45 silver badges75 bronze badges 2
  • 1 Have you ruled out CORS as being a problem? stackoverflow./questions/19554414/angularjs-disabling-cors – miqh Commented Jul 3, 2014 at 2:19
  • 1 Pretty sure it's a CORS request. Not sure why api.host would even be there if the request was to the same server that hosted the app. It makes perfect sense for the API provider to NOT allow CORS too, since it seems to be doing token authentication over an unsecured channel. – ivarni Commented Jul 3, 2014 at 4:26
Add a ment  | 

2 Answers 2

Reset to default 1

I think if you take a look at the link you refer to, you are getting the same issue as the author and the author of that post did state that it was a CORS issue - the server he was municating with didn't support CORS, he confirms this in his ment on his own post.

As for why you are getting an OPTIONS request, this is because CORS makes a "pre-flight" request to the server to establish if CORS is going to be supported, before it will make the actual request.

You are seeing a OPTIONS request most probably because you are making a cross origin request (CORS). Firstly your server should support CORS (by responding to OPTIONS request with a Access-Control-Allow-Origin header set in the response).

If you are making a authenticated cross origin request, you need to set withCredentials to true when making the request.

$http.get(url, { withCredentials: true })
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745062377a282776.html

最新回复(0)