I'm a newbie in angular. I'm trying to send a GET request to server with basic authentication headers, but it didn't work. Here is my code,
///////////////My app.js///////////////
var myApp = angular.module('myApp', ['ngRoute', 'ui.bootstrap']);
myApp.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider){
$httpProvider.defaults.headers.common['Authorization'] = 'Basic YWRtaW46YWRtaW4=';
$routeProvider
.when('/', {
controller: 'UserController',
templateUrl: './app/views/homepage.html'
})
.when('/users', {
controller: 'UserController',
templateUrl: './app/views/userlist.html'
});
}]);
///////////////My userService.js///////////////
myApp.factory('UserService', function($http){
var factory = {};
var urlBase = 'http://xxx.xxx.xxx.xxx/api';
factory.getUsers = function(){
return $http.get(urlBase+'/getusers');
};
return factory;
});
When I called the service from controller, There were 2 errors shown in the browser's console,
- OPTIONS http://xxx.xxx.xxx.xxx/api/getusers
- XMLHttpRequest cannot load http://xxx.xxx.xxx.xxx/api/getusers. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.
While I send request via POSTMAN app, it work normally.
Thanks for help.