views:

444

answers:

2

I've been sitting with this for hours now, and I cant understand why.

  • q is working. The URL does give me a proper JSON-response. It shows up as objects and arrays and whatnot under the JSON tab under the Net-tab in Firebug and all is fine. I've also tried with other URLs that i know work. Same thing happens.

  • I have another function elsewhere in my tiny app, wihch works fine, and is pretty much exactly the same thing, just another API and is called from elsewhere. Works fine, and the data variable is filled when it enters the getJSON-function. Here, data never gets filled with anything.

  • I've had breakpoints on every single line in Firebug, with no result. Nothing happens. It simply reaches the getJSON-line, and then skips to the debugger-statement after the function.

    var usedTagCount = 10;
    var searchHits = 20;
    var apiKey = "a68277b574f4529ace610c2c8386b0ba";
    
    
    var searchAPI = "http://www.flickr.com/services/rest/?method=flickr.photos.search&" + 
       "format=json&api_key=" + apiKey + "&sort=interestingness-desc&per_page="
        + searchHits + "&jsoncallback=?&nojsoncallback=1&tags=";
    
    
    var tagString = "";
    var flickrImageData = new Array();
    
    
    function search(query) {
     for(var i = 0; i < usedTagCount; i++) {
      tagString += query[i].key + ",";
     }
    
    
    
    var q = searchAPI + tagString;
    
    
    $.getJSON(q, function(data) { 
     debugger; /* It never gets here! */
    
    
     $.each(data.photos.photo, function(i, item) {
      debugger;
      flickrImageData.push(item);    
     });
    });
    
    
    debugger;
    return flickrImageData;
    
    }

Example of request URL (q):

http://www.flickr.com/services/rest/?method=flickr.photos.search&amp;format=json&amp;api_key=a68277b574f4529ace610c2c8386b0ba&amp;sort=interestingness-desc&amp;per_page=20&amp;jsoncallback=?&amp;tags=london,senior,iphone,royal,year,security,project,records,online,after,

I do wonder, since JSONView (the firefox plugin) cannot format it properly, that it isn't really JSON that is returned - the mime-type is text/html. Firebug, however, interprets it as JSON (as i stated above). And all the tag words come from another part of the app.

+1  A: 

I think you might need to remove the

nojsoncallback=1

from your searchAPI string.

Flickr uses JSONP to enable cross domain calls. This method requires the JSON to be wrapped in a json callback, the nojsoncallback=1 parameter removes this wrapping.

EDIT: Apparently it works with nojsoncallback=1, I got this piece of code to work for me. What jQuery version are you using? JSONP is only available from 1.2 and up.

This works for me (slight modifications):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">

var usedTagCount = 1;
var searchHits = 20;
var apiKey = "a68277b574f4529ace610c2c8386b0ba";


var searchAPI = "http://www.flickr.com/services/rest/?method=flickr.photos.search&amp;" + 
                    "format=json&api_key=" + apiKey + "&sort=interestingness-desc&per_page="
                     + searchHits + "&jsoncallback=?&nojsoncallback=1&tags=";


var tagString = "";
var flickrImageData = new Array();


function search(query) {
tagString = query;


var q = searchAPI + tagString;


$.getJSON(q, function(data) {   


    $.each(data.photos.photo, function(i, item) {
            debugger;
            flickrImageData.push(item);                          
    });
});

}

search("cat");

</script>
Oscar Kilhed
Tried it, changes nothing - sadly. I have also tried with other APIs (from Twitter), and it still doesn't work.
Arve Systad
jQuery 1.3.2, so that should work. But did the code run for you? It entered the function and filled the flickrImageData array with something? If so, would you be so kind and put it into pastebin for me to look at? :-)
Arve Systad
The piece of code in my answer works for me. It fills flickerImageData. I just changed some things to make it easier for me to debug (query is no longer an array).
Oscar Kilhed
Ok, it seems that it have been working all the time - I just had to disable Firebug. Kind of embarrassing, really. Does not work when firebug is enabled and i have break points there, but it works without. Thanks for your time anyways!
Arve Systad
Just glad I could help in some way.
Oscar Kilhed
A: 

When I try the url: http://www.flickr.com/services/rest/?method=flickr.photos.search&amp;format=json&amp;api%5Fkey=a68277b574f4529ace610c2c8386b0ba&amp;sort=interestingness-desc&amp;per%5Fpage=10&amp;tags=mongo

it returns data, as it should - try to change the getJSON to an $.ajax() and define a function jsonFlickrApi (data) with the code you have in you callback function.

If that don't work - please post code to at jsbin.com <- so we can try it live - so much easier to debug.

Eivind