Allison Stafford Allison Stafford - 6 months ago 12
jQuery Question

Why can't I call function with get request on click?

Is this a browser quirk, or am I using get incorrectly? I read through the api docs, but wasn't able to find information on why I wouldn't be able to use it this way. I can copy and paste the link into my browser and it returns a different quote every time.

Right now, when I call this function, and it calls itself again, it will infinitely print out the same quote over and over, instead of generating a new quote.

function apiCall() {
$.get('http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1', function(data) {
if (data[0].content.length > 100) {
apiCall();
}

console.log(data[0].content);
})

}

Answer

Use the ajax get method to turn off cache:-

$.ajax({
  url: "'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1",
  cache: false,
  success: function(data){
    if (data[0].content.length > 100) {
      apiCall();
    }
    console.log(data[0].content);
  }
});
Comments