Little help with $.getJSON - quote generator project

Little help with $.getJSON - quote generator project
0

#1

Hi - my $.getJSON method was working yesterday but I’ve obviously tweaked something… I’ve commented most of the code, just trying to get alert boxes to appear. I’ve tested the link in my browser and it seems to work fine, returns a json object as expected. When the user selects #getNewQuote (refresh icon) it calls the function successfully (the first alert is triggered), but the second alert in the $.getJSON method never shows. Checked settings but can’t see anything wrong there - Any help much appreciated as I’m going a little nutty.


#2

Never mind - just came back to it and it started working. Which is a little disturbing. Thanks to anyone who had a look.


#3

Your $.getJSON is returning a quote, but there’s nothing inside the success function so you’re not doing anything with the returned object.
It should look something along these lines…

$.getJSON(URL).done(function(data){
$('#quote-box').text(data.quote);
})

So inside of your .done() function (which is the function that is run when the callback returns successfully) you can manipulate the data that is returned and display it on your page. In this example $(’#quote-box’) would be the element in which you’d display the quote.
You can also chain a .fail() method to the above which would run if the callback was unsuccessful for any reason. It would look like this…

.fail(function(e){
$('#quote-box').text("Quote could not be returned");
});

Make sure there is no semi-colon at the end of the .done() function or .fail() will not chain.


#4

Thanks so much for reviewing it. I had stripped out the code because I just couldn’t get it to work - so went bare bones with an alert. I just wanted to see any sign of life within the getJSON method. It started working about 30 mins after I posted - though I hadn’t changed anything which is a mystery (sorry - did try to flag it as solved with my reply). Your reply did introduce me to the done and fail callbacks - much appreciated.