1

I have issue when try to show the data I get. Here is my code:

$http({
    method:'get',
    format:'json',
    url:'http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json',        
}).success(function(data, status, headers, config){
    $scope.photoID = data;
        alert($scope.photoID)

})

The Json file I get:

jsonFlickrApi(
{
photos: {
page: 1,
pages: 34,
perpage: 30,
total: 1000,
photo: [
{
id: "11480313795",
owner: "80249365@N00",
secret: "d4950d1c38",
server: "5482",
farm: 6,
title: "5DM38945",
ispublic: 1,
isfriend: 0,
isfamily: 0
},.....

And in this situation it can show the whole json file in alert window. However when I want to get some specific data like page or photo id,it just shows undefined or even error in the console.

$http({
        method:'get',
        dataType:'jsonp',
        url:'http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json&jsoncallback=?',     
    }).success(function(data, status, headers, config){
            $scope.photoID = data.photos.page;
            alert($scope.photoID);

    })

I can ajax the same json file with Jquery,so I have no idea what's wrong. Can anyone help me please?

  • I think this is a duplicate: http://stackoverflow.com/questions/12066002/parsing-jsonp-http-jsonp-response-in-angular-js – Neikos Dec 21 '13 at 15:40
  • You also just published your private api key? Unless that is a public one? I am not sure about Flickr – Neikos Dec 21 '13 at 15:40
  • @Neikos It doesn't help,but thx. And also for your remind,I hide my api keys. – user3125499 Dec 21 '13 at 16:07

1 Answers1

2

You are using the $http.get method. The flickr api you are using returns a JSONP format though. This is what seems to make your code not work. Check out my original answer for the correct way to handle this in this case.

Previous Answer:

From the $http docs, your call has to contain a JSON_CALLBACK substring.

Yours is missing.

The correct url should be:

http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json&jsoncallback=JSON_CALLBACK

EDIT:

Here is a plunkr, with a test case

http://plnkr.co/edit/MUMnHML5QtDJTthmtZ2Y?p=preview

Code:

$http.jsonp("http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=KEY&per_page=30&format=json&jsoncallback=JSON_CALLBACK")
.success(function(data) {

  $scope.data = data.photos;

})
Neikos
  • 1,840
  • 1
  • 22
  • 35
  • I tried your method but still have same problem. I can get the data,and show them all.But when i trying to set what data i want(like data.photos.photo.id), it just keep showing undefined. – user3125499 Dec 21 '13 at 16:18
  • There added a plunkr. Be sure to replace the key with your own, that should work. – Neikos Dec 21 '13 at 16:28
  • It works!!!But I still don't know where I did wrong. I use the same url in both your code and mine,but my code just dont work.Do you see any mistake I made in my code? Thx very much. – user3125499 Dec 22 '13 at 03:19
  • at the end, you used `json_callback=?` instead of `json_callback=JSON_CALLBACK` – Neikos Dec 22 '13 at 06:34