0

I have looked through here and I realize it doesn't look like getJSON returns anything but a object that is unusable. The problem I am having is that I am trying to edit someone else's code to pull picture from flickr. I am trying to make a function that will return the description of the pictures. I know that it doesn't return information however there has to be a way to update a global variable or somehow pass off the information i need into another variable to return to his function. This is the jist of what I have so far.

  function add_description(n){

    var img_id = String(n);


    var textInfo ="";
$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){


                 textInfo = String(data.photo.description._content);
                alert(textInfo);
                return textInfo;           

            })


}

this is the code I tried after your updates George. Thanks!

 var testObj=$.getJSON('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=36c8b00c47e8934ff302dcad7775d0a2&photo_id='+img_id+'&format=json&jsoncallback=?', function(data ){



                 textInfo = String(data.photo.description._content);
                alert(textInfo);
                return textInfo;



            })
Elliott
  • 539
  • 4
  • 14

2 Answers2

1

The first step is to assign the results to the textInfo variable:

$.getJSON(...) {
    textInfo = String(data.photo.description._content);

The $.getJSON() call is asynchronous, so your code return textInfo; probably runs before the $.getJSON() call completes. Therefore, the textInfo variable is still an empty string. You will need to call the other code from within the $.getJSON() call, or delay execution of the return until the asynchronous call completes.

This answer is based on the similar answer found here.

EDIT (based on your question update):

You will not be successful including the return statement within scope of $.getJSON(). You can assign the result to the variable textInfo as you have done, and that value will be available when $.getJSON completes.

However, you must ensure that the call has completed before attempting to accessing the value. You can use setTimeout(), which seems messy, or you can substitue $.ajax() for your $.getJSON() call so you can use the 'async = false' option. This will force the call to complete before execution continues, allowing the variable to be populated before returning it.

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • Thats because the link is dynamically taking in the img-id. I know it pulls the information correctly. I have debugged it and seen it getting the description like I am asking it to. – Elliott Jul 12 '11 at 19:18
  • George as I see that this could a problem. I tried many different ways, like one instance I had the return inside the call the JSON and what I am getting out of JSON is merely "null" and not blank which is what I would get if it isn't setting the variable properly. – Elliott Jul 12 '11 at 19:36
  • The return within the .getJSON call will only return that scope, not from the parent scope ('add_description'). Can you post the code that you used that was similar to what I described so we can determine what caused the value of `textInfo` to be null? – George Cummins Jul 12 '11 at 19:38
0

The photo doesn't exist..

console.log(data);

Output:

code: 1
message: "Photo "2121" not found (invalid ID)"
stat: "fail"
Eddie
  • 12,898
  • 3
  • 25
  • 32