Jared Blumer
Jared Blumer

Reputation: 7

getJSON Works on Document Load, but not through Button Click

The function getQuote successfully populates the quote and author divs with text when the document is ready, but it does not pull a new quote and author on the button click. I tested the button click with an alert function and it works so I'm not sure why the getQuote function has no results.

var quote = "", author = "";

function getQuote() {
  $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(data) {
  quote = data[0].content;
  document.getElementById("quote").innerHTML=quote;
  author = data[0].title;
  document.getElementById("author").innerHTML="- " + author;
  });
}

$(document).ready(function() {
  getQuote();
  $("#button").click(getQuote);
});

Upvotes: 0

Views: 49

Answers (1)

Jared Blumer
Jared Blumer

Reputation: 7

It turns out to be a cache issue. Problem solved:

$.ajaxSetup({
         cache: false
})

Upvotes: 0

Related Questions