简体   繁体   中英

AngularJS and Basic HTTP Auth

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:password@build.com/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://username@build.com/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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM