0

I am trying to use AngularJS with an external API. The API uses HTTP Basic for authentication.

I have looked around at how to do this but nothing seems to work. The OPTIONS request is always sent without the Authorization header.

Here is some example code:

 var app = angular.module("BE", []);

        app.controller("NextCtrl", function ($scope, $http) {
            $http.defaults.headers.common['Authorization'] = 'Basic encoded_credentials';
            $http.get('http://api-url.com').
                success(function(data, status, headers) {
                  $scope.event = data.Event;
                  $scope.race = data.Race;
                  $scope.entrants = data.Entrants
                }).
                error(function(data, status, headers, config) {
                  // log error
            });

        });

How can I make my requests with the Authorization header set for every request?

1 Answer 1

1

You can't control that because OPTIONS is sent by a browser. A browser removes any authorization data in OPTION requests (see CORS W3C Recommendation).

In most cases Authorization header for OPTIONS-requests isn't needed because the response includes only CORS headers and doesn't perform any action.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.