Answer the question
In order to leave comments, you need to log in
AngularJS + $httpProvider + CORS - what's wrong?
Hello!
I am making an application in AngularJS. I ran into a CORS issue when trying to use Angular-UI Typeahead and loading cities from the google map api.
There is following app.js code
.config(
[ '$stateProvider', '$urlRouterProvider', '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$httpProvider',
function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $httpProvider) {
// lazy controller, directive and service
app.controller = $controllerProvider.register;
app.directive = $compileProvider.directive;
app.filter = $filterProvider.register;
app.factory = $provide.factory;
app.service = $provide.service;
app.constant = $provide.constant;
app.value = $provide.value;
$httpProvider.interceptors.push('TokenInterceptor');
//
.factory('TokenInterceptor', function ($q, $window, $location, AuthenticationService) {
return {
request: function (config) {
config.headers = config.headers || {};
if ($window.sessionStorage.token) {
config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
}
return config;
},
requestError: function(rejection) {
return $q.reject(rejection);
},
/* Set Authentication.isAuthenticated to true if 200 received */
response: function (response) {
if (response != null && response.status == 200 && $window.sessionStorage.token && !AuthenticationService.isAuthenticated) {
AuthenticationService.isAuthenticated = true;
}
return response || $q.when(response);
},
/* Revoke client authentication if 401 is received */
responseError: function(rejection) {
if (rejection != null && rejection.status === 401 && ($window.sessionStorage.token || AuthenticationService.isAuthenticated)) {
delete $window.sessionStorage.token;
AuthenticationService.isAuthenticated = false;
$location.path("/access/login");
}
return $q.reject(rejection);
}
};
})
Request from foreign origin blocked: Single origin policy prevents reading remote resource at http://maps.googleapis.com/maps/api/geocode/json?a... This can be fixed by moving the resource to the same domain or enabling CORS .
<div class="form-group">
<label>Местоположение</label>
<div class="pos-rlt" ng-controller="TypeaheadLocationCtrl">
<i ng-show="loadingLocations" class="fa fa-spinner fa-spin form-control-spin"></i>
<input type="text" ng-model="userinfo.location" placeholder="Введите первые буквы названия местоположения" typeahead="address for address in getLocation($viewValue)" typeahead-loading="loadingLocations" class="form-control">
</div>
</div>
// TypeaheadLocation
.controller('TypeaheadLocationCtrl', ['$scope', '$http', function($scope, $http) {
$scope.selected = undefined;
// Any function returning a promise object can be used to load values asynchronously
$scope.getLocation = function(val) {
return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: val,
sensor: false
}
}).then(function(res){
var addresses = [];
angular.forEach(res.data.results, function(item){
addresses.push(item.formatted_address);
});
return addresses;
});
};
}])
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
Answer the question
In order to leave comments, you need to log in
See, Google allows requests from any site by returning a header in the Access-Control-Allow-Origin: * response. And your authorization works only thanks to TokenIntercepter, it is he who substitutes the authorization token for all your requests. I checked, Google is working fine, if you put an extra Authorization header on it, then it's definitely not the request Intersepter
request: function (config) {
config.headers = config.headers || {};
if ($window.sessionStorage.token) {
config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
}
return config;
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question