【问题标题】:AngularJS - Calling Flickr API fails with warning messageAngularJS - 调用 Flickr API 失败并显示警告消息
【发布时间】:2013-05-22 11:26:53
【问题描述】:

我有一个简单的 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;
        }
    }
})

;

【问题讨论】:

  • jsonp 请求也适用于您的情况

标签: javascript angularjs flickr browser-security


【解决方案1】:

X-Requested-With 请求标头会导致问题 - see this question for more information。下面的代码应该可以解决这个问题:

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

【讨论】:

  • 我在 angular.module('photoApp').factory... 部分之后添加了您的代码,但我仍然收到相同的消息。我需要在其他地方添加它吗?
  • 不,这应该可以解决问题,配置在工厂方法之前运行 - 我创建了这个在 chrome 中工作的 plunker:plnkr.co/edit/TQrCBl51iz7Ecg2POxNM?p=preview
  • 感谢您的支持!我根据您的 plunker 更新了我的代码 - 请参阅我编辑的问题。它在 Chrome 和 FF 中运行良好,但 IE 仍然抛出警告消息。任何想法如何摆脱消息?
  • 我不认为有任何简单的解决方案。它在 IE10 中运行良好,但 CORS 在旧版本的 IE 上存在一些问题:github.com/angular/angular.js/pull/1047 - 如果可能,我建议使用 jsonp
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2017-08-19
    • 2017-08-12
    • 2016-12-05
    相关资源
    最近更新 更多