簡體   English   中英

Angularjs Flickr API語法錯誤:使用nojsoncallback = 1時出現意外令牌

[英]Angularjs Flickr API SyntaxError: Unexpected token when using nojsoncallback=1

我有一個angularjs應用程序來調用flickr api。

我想要沒有功能包裝並且按照docs的 RAW json格式的數據,應用&nojsoncallback=1

但是我收到以下控制台錯誤。 SyntaxError: Unexpected token '

僅在將&nojsoncallback=1應用於網址時才會出現此錯誤。 但是我想要沒有包裝的RAW json。

如果我不將以上內容應用於url並簡單使用https://api.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json我不會收到任何錯誤,但是在控制台注銷typeof時顯示“字符串”。

然后,我嘗試將其解析為JSON並收到另一個錯誤,因為它已包裝。 因此,為什么我要RAW。

下面是我到目前為止的代碼。 任何幫助-非常感謝。

JS

(function(){
'use strict';

    var app = angular.module('flickrApp', []);

    app.controller('FlickrFeedController', ['$http', '$scope', function($http, $scope){

        // grab the flickr api
        var response = $http.get('http://crossorigin.me/https://api.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json&nojsoncallback=1');

        // on success
        response.success(function(data){

            // console logging out the typeof gives 'string'
            console.log(typeof(data));

            // since it's a string I would then want to convert it into a json object
            // but I need to sort the current error out first
            // data = JSON.parse(data);
            // console.log(typeof(data));

        });
    }]);

})();

編輯 :這是一種解決方法&nojsoncallback=1從網址中刪除&nojsoncallback=1 (刪除控制台錯誤),並且由於數據以字符串形式返回,必須替換字符,然后進行解析。 不太好,但是我得到了所需的輸出(對象),並認為我會將其添加到這里供其他人查看。

JS

(function(){
'use strict';

    var app = angular.module('flickrApp', []);

    app.controller('FlickrFeedController', ['$http', '$scope', function($http, $scope){

        // grab the flickr api
        var response = $http.get('http://crossorigin.me/https://api.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json');

        // on success
        response.success(function(data){

            // typeOf is 'string' even though format=json is specified in the url
            //console.log(typeof(data));
            //console.log(data);

            // work-around since data is returned as a string
            data = data.replace('jsonFlickrFeed(', '');
            data = data.replace('})', '}');
            data = data.replace(/\\'/g, "'");

            // parse the data
            data = JSON.parse(data);

            // typeOf is 'object'
            console.log(data.items);
            console.log(typeof(data));

        });
    }]);

})();

生成角度資源以使用以下格式調用api: {'json', jsoncallback: 'JSON_CALLBACK'} 在此處檢查完整的解決方案-http: //plnkr.co/edit/Lxxkb9?p=preview

var app = angular.module('flickrApp',['ngResource']);

app.factory('Flickr', function($resource, $q) {
  var photosPublic = $resource('http://crossorigin.me/https://api.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json', 
      { format: 'json', jsoncallback: 'JSON_CALLBACK' }, 
      { 'load': { 'method': 'JSONP' } });
  return {
    get: function() {
      var q = $q.defer();
      photosPublic.load(function(resp) {
        q.resolve(resp);
        console.log(resp.items);
      })
      return q.promise;
    }
  }
});
app.controller('FlickrCtrl', function($scope, Flickr) {
Flickr.get();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM