Getting problem to connect with random quote generator API

Getting problem to connect with random quote generator API
0

#1

when Iam clicking the button , it is not showing any result.
here is my js code

function randomQuote() {
$.ajax({
url:"http://api.forismatic.com/api/1.0/?",
dataType: "jsonp",
data:"method=getQuote&format=jsonp&lang=en&jsonp=?",
success: function( response ) {
$("#random_quote").html("<p id='random_quote' class='text-center'>" +
response.quoteText + "<br/>&dash; " + response.quoteAuthor + " &dash;</p>");
}
});
}
$(document).ready(function() {randomQuote();
$("button").click(function(){
randomQuote();
});
});

#2

I just tried your code and it works. Show the whole code.
If you are writing the code in Codepen make sure that you have added jQuery in JS settings.


#3

I am giving you my codepen link
http://codepen.io/ran91/pen/YpJmYM


#4

You’ve apparently fixed it on your own already. Works for me.
One minor thing: In your twitter function where you add the quotation marks, you might want to add a space so the author name does not come directly after the closing quotation mark. Like so: + '" ' +.


#5

ok…Thank you …I will fix it.


#6

Hi, it looks like your problem is fixed, but I have a question because I am using the same API (forismatic.com) for the quote generator. How did you know to structure part of your data in ajax as “jsonp=?” ? I was stuck on this until I saw your solution because I was using the data like this: “callback=?” and not “jsonp=?”.

I kept getting a syntax error even though other sources told me to use “callback=?” at the end of a JSONP request. Thanks for any insight.


#7

I got the idea of this from here https://github.com/FreeCodeCamp/FreeCodeCamp/issues/3488


#8

Very good resource. Thanks, and good luck coding!


#9

I am having the same problem. Quotes are not getting displayed . My code is:

$(document).ready(function() {
var randomQuote;
var randomAuthor;
function getQuote() {
var url = "http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?";

$.getJSON(url, function(data) {
  $(".quote").html(data.quoteText);
  $(".author").html("~" + data.quoteAuthor);
  randomQuote = data.quoteText;
  randomAuthor = data.quoteAuthor;
});

}
$("#newQuote").on(“click”, function() {
getQuote();
});

$("#tweet").on(“click”, function() {
window.open(“https://twitter.com/intent/tweet?text=
+randomQuote + " ~" + randomAuthor);
});
});


#11

Thank you very much. It worked.
The problem was with s. It should have been https instead of http. but what is exactly it means, i don’t get it. Can you eleborate a bit?


#13

Thanks for the explanation. This was new to me.