Angular $ http请求问题

时间:2015-06-27 14:59:32

标签: angularjs

我正试图从flickr服务this one中检索一些图像。 不幸的是,我有以下错误:

XMLHttpRequest cannot load http://api.flickr.com/services/feeds/photos_public.gne?format=json. 

No 'Access-Control-Allow-Origin' header is present on the requested resource. 

Origin 'http://localhost:3000' is therefore not allowed access.

这是我的代码:

angular.module('myApp')
  .service('getFlickrImages', function($http){
    this.getData = function() {
      delete $http.defaults.headers.common['X-Requested-With'];
        return $http({
            method: 'GET',
            url: 'http://api.flickr.com/services/feeds/photos_public.gne?format=json'
         });
     }

  });

angular.module('myApp')
  .controller('MainCtrl', function ($scope, getFlickrImages) {
      $scope.data = null;
      getFlickrImages.getData().then(function(dataResponse) {
       $scope.data = dataResponse;
   });
  });

如果有人可以帮助我,那么Plunker

1 个答案:

答案 0 :(得分:0)

我终于找到了chrome插件:

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?utm_source=chrome-ntp-icon

安装它,激活它并刷新视图。

如果没有Access-Control-Allow-Origin问题,它将至少让您工作。 如果有人带来修复但直接在代码中会很好......