How to get the correct JSON object from flickr API

I used the flickr photo search method to get public photos. when I run it with jquery it works fine, I get the json object in the correct form.

{ "photos": { "photo": [ { ......... } ] }, "stat": "ok" } 

But when I use it with AngularJs, I got the same object with the jsonFlickrApi prefix

 jsonFlickrApi({ "photos": { "photo": [ { ...... } ] }, "stat": "ok" }) 

what I used in AngularJs:

 myApp.service('dataService', function($http) { delete $http.defaults.headers.common['X-Requested-With']; this.flickrPhotoSearch = function() { return $http({ method: 'GET', url: 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=3f807259749363aaa29c2fa93945&tags=india&format=json&callback=?', dataType: 'json' }); } }); 

Please tell me how I can convert the second JSON to the first. Is there anything I can do in the $http call or modify the JSON object.

+8
json javascript jquery angularjs flickr
source share
3 answers

There was a problem in the url. This url works for me.

 https://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key=3f807259749363aaa29c712fa93945&user_id=61495424@N00&format=json&nojsoncallback=? 
+17
source share

You should add nojsoncallback = 1 to the url like this

 https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=XXX&format=json&nojsoncallback=1 

And remove the jsonFlickrApi function when responding.

+9
source share

You get a JSONP response (note P), which is basically a function call with an argument, which is the answer you are expecting.

A simple solution for you: create a function called jsonFlickrApi with the response parameter, and you can do your transfer there.

At this point, I'm not sure if you can define your function inside your service, but give it a try. If not, you can define it outside.

Alternatively, try using the $ http.get function instead. Did you specify the return accepted by the client in jQuery source code?

EDIT

Try setting the request data type using $ http or $ http.get , remembering to specify the data property in the settings object.

 $http({ url: 'http://localhost:8080/example/teste', dataType: 'json', method: 'POST', data: '', headers: { "Content-Type": "application/json" } }).success(function(response){ $scope.response = response; }).error(function(error){ $scope.error = error; }); 
+1
source share

All Articles