I have a simple AngularJS app that allows one to search for Flickr photos. The problem is in IE I get the the following message when I call the Flickr API:

This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?

If I click Yes, the app works and loads the relevant photos. However, in Chrome and Firefox I do not get any message and nothing happens - no photos are loaded.

Here is the code:

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;
        }
    }
});

How do I get rid of the message and make it work in Chrome/Firefox?

UPDATE: I changed the code to the below based on joakimbl's plunker and it now runs in Chrome and FF but IE still throws the warning message.

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;
        }
    }
})

;

有帮助吗?

解决方案

The X-Requested-With request header causes problems - see this question for more information. The following code should fix the problem:

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

其他提示

I faced the same issue with IE10. I added the url which makes CORS call as a trusted site in IE10 and it worked fine. I suggest you do the same. Perhaps it will help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top