Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random movie quote generator jquery ajax call to api and tweet button to tweet out quote.
$(document).ready(function(){
//get the quote api information with Ajax
getQuote();
var bgCols = ['#2e67f7','#ea5c30','#38b582','#683a20','#59357d', '#555'];
function getQuote(){
var num = Math.floor(Math.random()*6+1);
$.ajax({
url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=1",
type: "GET",
data: {},
dataType: "json",
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "xYdGlgmLb6mshjBkNyNcbyQpGZuLp17o5c4jsnMmdf7VIAQut0"); // My Mashape key
},
success: function (data){
$('.quoteline').html(data.quote);
$('.author').html(data.author);
$('.twitter-share-button').attr("href",'https://twitter.com/intent/tweet?text='+ encodeURIComponent(data.quote) + encodeURIComponent(' Movie: ') + encodeURIComponent(data.author) + encodeURIComponent(' #FreeCodeCamp #RandomQuoteGenerator') + '&via=Nicki_Hastings');
$('.container-box').css('background-color', bgCols[num]);
},
error: function(err) { console.log("Failure!..."); console.log(err); }
});
}
$("#nextQuote").on("click", function(e){
e.preventDefault();
getQuote();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment