3

我有一个简单的 AngularJS 应用程序,它允许人们搜索 Flickr 照片。问题出在 IE 中,当我调用 Flickr API 时收到以下消息:

此页面正在访问不受其控制的信息。这会带来安全风险。你要继续吗?

如果我单击是,该应用程序将运行并加载相关照片。但是,在 Chrome 和 Firefox 中,我没有收到任何消息,也没有任何反应 - 没有加载照片。

这是代码:

function PhotoController($scope, photoData) {
    $scope.thumbSize = 'small';
    $scope.setThumbSize = function (size) { $scope.thumbSize = size; };

    $scope.submitSearch = function getPhotos() {
        $scope.photos = [];
        $scope.items = [];

        photoData.getAllItems($scope.searchKeyword).then(function (data) {
            var parsedData = angular.fromJson(data);
            $scope.items = parsedData.photos.photo;

            for (var i = 0; i < $scope.items.length; i++) {
                var photo = $scope.items[i];
                $scope.photos.push({ title: photo.title, thumbUrl: ' http://farm' + photo.farm + '.staticflickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_m.jpg' });
            }
        },
        function (errorMessage) {
            $scope.error = errorMessage;
        });

    };
}

angular.module('photoApp').factory('photoData', function ($http, $q) {
    return {
        getAllItems: function (keyWord) {
            //Creating a deferred object
            var deferred = $q.defer();
            var apiUrl = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=myAPIkey&tags=' + keyWord + '&format=json&nojsoncallback=1';

            //Calling Web API to fetch pics
            $http.get(apiUrl).success(function (data) {
                deferred.resolve(data);
            }).error(function () {
                deferred.reject("An error occured while fetching photos");
            });
            return deferred.promise;
        }
    }
});

如何摆脱该消息并使其在 Chrome/Firefox 中工作?

更新:我根据 joakimbl 的 plunker 将代码更改为以下代码,现在它在 Chrome 和 FF 中运行,但 IE 仍然抛出警告消息。

var app = angular.module("photoApp", []);

app.controller('PhotoController', function ($scope, photoData) {
    $scope.thumbSize = 'small';
    $scope.setThumbSize = function (size) { $scope.thumbSize = size; };

    $scope.submitSearch = function getPhotos() {
        $scope.photos = [];
        $scope.items = [];

        photoData.getAllItems($scope.searchKeyword).then(function (data) {
            var parsedData = angular.fromJson(data);
            $scope.items = parsedData.photos.photo;

            for (var i = 0; i < $scope.items.length; i++) {
                var photo = $scope.items[i];
                $scope.photos.push({ title: photo.title, thumbUrl: ' http://farm' + photo.farm + '.staticflickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_m.jpg' });
            }
        },
        function (errorMessage) {
            $scope.error = errorMessage;
        });
    };
});

app.config(function ($httpProvider) {
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

app.factory('photoData', function ($http, $q) {
    return {
        getAllItems: function (keyWord) {
            //Creating a deferred object
            var deferred = $q.defer();
            var apiUrl = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=84ad829261f6347dbfc4bf23fc1afdbd&tags=' + keyWord + '&format=json&nojsoncallback=1';

            //$http.defaults.useXDomain = true;
            //delete $http.defaults.headers.common['X-Requested-With'];

            //Calling Web API to fetch pics
            $http.get(apiUrl).success(function (data) {
                //Passing data to deferred's resolve function on successful completion
                deferred.resolve(data);
            }).error(function (error) {
                //Sending a friendly error message in case of failure
                deferred.reject("An error occured while fetching items");
            });
            //Returning the promise object
            return deferred.promise;
        }
    }
})

;

4

2 回答 2

4

X-Requested-With请求标头会导致问题 -有关详细信息,请参阅此问题。下面的代码应该可以解决这个问题:

angular.module('photoApp').config(function($httpProvider){
  delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
于 2013-05-22T12:03:30.397 回答
1

我在 IE10 上遇到了同样的问题。我在 IE10 中添加了使 CORS 调用作为受信任站点的 url,它运行良好。我建议你这样做。也许它会有所帮助。

于 2013-11-27T14:44:14.910 回答