简体   繁体   English

Angularjs Flickr API语法错误:使用nojsoncallback = 1时出现意外令牌

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

I have an angularjs app to call a flickr api. 我有一个angularjs应用程序来调用flickr api。

I want the data in RAW json format with no function wrapper and as per the docs , applying &nojsoncallback=1 . 我想要没有功能包装并且按照docs的 RAW json格式的数据,应用&nojsoncallback=1

However I'm getting the following console error. 但是我收到以下控制台错误。 SyntaxError: Unexpected token '

This error only appears when applying &nojsoncallback=1 to the url. 仅在将&nojsoncallback=1应用于网址时才会出现此错误。 However I want RAW json with no wrapper. 但是我想要没有包装的RAW json。

If I don't apply the above to the url and simple use https://api.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json I get no error, but when console logging out the typeof I get 'string' displayed. 如果我不将以上内容应用于url并简单使用https://api.flickr.com/services/feeds/photos_public.gne?tags=trees&format=json我不会收到任何错误,但是在控制台注销typeof时显示“字符串”。

I then try parsing this into JSON and get another error because it has a wrapped. 然后,我尝试将其解析为JSON并收到另一个错误,因为它已包装。 Hence why I want RAW. 因此,为什么我要RAW。

Below is the code I have so far. 下面是我到目前为止的代码。 Any help - much appreciated. 任何帮助-非常感谢。

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

        });
    }]);

})();

EDIT : This is a work around removing &nojsoncallback=1 from the url (removing the console error) and since the data comes back as a string having to replace characters, then parse. 编辑 :这是一种解决方法&nojsoncallback=1从网址中删除&nojsoncallback=1 (删除控制台错误),并且由于数据以字符串形式返回,必须替换字符,然后进行解析。 Not great but I get the required output (object) and thought I'd add it up here for others to view. 不太好,但是我得到了所需的输出(对象),并认为我会将其添加到这里供其他人查看。

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

        });
    }]);

})();

Generate angular resource to call the api with format: {'json', jsoncallback: 'JSON_CALLBACK'} . 生成角度资源以使用以下格式调用api: {'json', jsoncallback: 'JSON_CALLBACK'} Check complete solution here - http://plnkr.co/edit/Lxxkb9?p=preview 在此处检查完整的解决方案-http: //plnkr.co/edit/Lxxkb9?p=preview

var app = angular.module('flickrApp', ['ngResource']); 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