Random Quote problem

Random Quote problem
0.0 0

#1
I am trying to use following code but it is not working. Please can you tell me where am I wrong.

   var Quote = function(){   //  object Quote
 
$.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?", function(data) {
  this.qText = data.quoteText;
  this.aText = data.quoteAuthor;
  
       
});   
  };
  
       
  function myQuote(){
  
  $(".quotetext").html("<h2>" + Quote.qText + "</h2>");
  $(".quoteauthor").html("<em>" + "-" + Quote.aText + "</em>");
  };
              
  myQuote();
  
  $("#getQuote").on("click", function myData() {
   myQuote();
  });

#2

Please format your code so we can see. You can do so by selecting the code, then click “Preformatted text” on the post editor (the button that looks like </>)


#3

Thanks @kevcomedia I have preformatted the text. Please advise the error in code.


#4

This will help you with your formatting code here: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code-and-syntax-highlighting

I do not know, but if I remember, you need to do what the myQuote() function does in the $.getJSON(). The $.getJSON is asynchronous, so myQuote() is run before you get the values. I think this may be the case, I cannot guarantee it for sure.

Also, are you getting any errors? Do you have a link to the project so we can see if other stuff is going on? Or is it not updating the text? please give a little more information than - this is my code show me error.


#5

Try putting the code that displays to the HTML inside the callback to .getJSON.


If you wanted to use constructors, you’ll have to do something like

var Quote = function() {...};
var q = new Quote();
$(...).html(q.qText);
$(...).html(q.aText);

However, wrapping .getJSON in a constructor won’t work because .getJSON is an async operation. Your code will continue to run while you are wating for the data from forismatic. So you’ll end up with qText and aText being used despite containing no data.


#6

Thanks I understand it is the problem with wrapping .getJSON in object.Thanks @kevcomedia


#7

I resolved the above issue by not using in object. Now I have problem with adding hashtag quote at the of tweeter text. It is not showing ‘#quotes’ at the end of text in tweeter window… See below the code.

window.open(‘https://twitter.com/intent/tweet?text=’+qText+’-’+aText+’#quotes’, ‘twitterwindow’, ‘height=255, width=550, top=’+($(window).height()/2 - 225) +’, left=’+($(window).width()/2 - 275 ) +’, toolbar=0, location=0, menubar=0, directories=0, scrollbars=0’)


#8

You’ll have to move the quotes out of the text parameter and move it to the hashtags parameter in your url. See the docs


#9

Thanks @kevcomedia adding hastag parameter as &hashtags=quotes is working.


#10

When getJSON from
http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=
Then i getting Cross-Origin XMLHttpRequest Error
But from
http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?
Not getting any Cross-Origin XMLHttpRequest Error.

Why ???