AngularJS and Basic HTTP Auth

214 views Asked by At

I have an Angular app that is accessing a test database. This has worked before, but over the weekend I'm no longer allowed access. I have made no changes to the Angular code. Here's the issue.

For me to access the test database, I have to pass in the username and password since the database is using Basic HTTP Auth. My resource ends up looking like this.

angular.module("CoolApp.misc")

.factory('Ad', ad)

ad.$inject = ['$resource'];

function ad($resource) {
  return $resource('http://username:[email protected]/api/advertisement.json');
}

Now when I run

Ad.get({}, function(resp) {}, function(badresp) {console.log(badresp)})

the console spits out the badresp object. I look inside my config headers section and notice this as the url...

url: "http://[email protected]/api/advertisement.json"

Wait a minute, I set the password in as the url. Why is the header not supplying me the password? Is that why I'm receiving a No 'Access-Control-Allow-Origin' header is present on the requested resource. in my console?

For kicks here is my http config

angular.module("CoolApp")

.config(config);

config.$inject = ["$httpProvider"];

function config($httpProvider) {
  $httpProvider.defaults.useXDomain = true;
  //$httpProvider.defaults.withCredentials = true;
  delete $httpProvider.defaults.headers.common["X-Requested-With"];
  $httpProvider.defaults.headers.common["Accept"] = "application/json";
  $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
}

My question is, what's wrong with this and how do I fix it?

1

There are 1 answers

0
Josh Beam On BEST ANSWER

No 'Access-Control-Allow-Origin' header is present on the requested resource. If you say you haven't changed the front-end code since the last time you touched it, then that can only mean that something on the server changed... Based on the error you've provided, it seems like a pretty clear case of somebody either removing the Access-Control-Allow-Origin on the server, or limiting the origins.

You can't change that on the client; that has to change on the server.