最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add access token in angularjs http - Stack Overflow

programmeradmin3浏览0评论

i am new in developing a website using angularJS as my client-side and i am trying to access my JSON thru API call with $http.get method for angular.

my issue is that my API call requires an Access Token. i always get this error whenever i try to refresh my page.

XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.

i am currently using this code to fetch my data.

myApp.controller('loginCtrl', function($scope, $http) {
  $http.get("")
    .then(function(response) {
      $scope.names = response.data.records;
    });
});

i am new in developing a website using angularJS as my client-side and i am trying to access my JSON thru API call with $http.get method for angular.

my issue is that my API call requires an Access Token. i always get this error whenever i try to refresh my page.

XMLHttpRequest cannot load http://unexus-api-dev-3urcgetdum.elasticbeanstalk./drugstores. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.

i am currently using this code to fetch my data.

myApp.controller('loginCtrl', function($scope, $http) {
  $http.get("http://unexus-api-dev-3urcgetdum.elasticbeanstalk./drugstores")
    .then(function(response) {
      $scope.names = response.data.records;
    });
});

how do i insert an access token on the $http.get method.

Share Improve this question asked Dec 21, 2015 at 8:40 George Terence TingGeorge Terence Ting 571 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

It depends on how your API get this access token. The most popular option is to just add specyfic header to your request

var req = {
   method: 'POST',
   url: 'http://example.',
   headers: {
      'Authorization': 'access token'
   },
}

$http(req).then(function(){...}, function(){...});

you can also add it globally

module.run(function($http) {
  $http.defaults.headers.mon.Authorization = 'Basic YmVlcDpib29w';
});

or by using Angular interceptors http://www.diwebsity./2015/12/17/interceptor-in-angular/

You are running into a SOP issue that is described here https://en.wikipedia/wiki/Same-origin_policy

You should have a look here:

  1. Ways to circumvent the same-origin policy
  2. Same origin policy
  3. XMLHttpRequest Same Origin Policy
发布评论

评论列表(0)

  1. 暂无评论