0

Using a Mashape API for random quotes, but nothing happening on click. Below is the JS and HTML. Is there something wrong with the JS code? When I click the button, nothing happens. The quote is not appearing in the div. Thanks!

$('#getQuote').click(function (){
        $.ajax({
          headers: {
            'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          method:'POST',
          dataType: 'json',
          url: 'https://andruxnet-random-famous-quotes.p.mashape.com/',
          success: function(response) {
            var ape = JQuery.parseJSON(response)
            var quoteText = ape.quote;
            var quoteAuthor = ape.author;
            $(".quote").html(quoteText);
            $(".author").html(quoteAuthor);}
        });
      });



<body>
  <div class = "quote">quote</div>
  <div class = "author">author</div>
  <div id="button">
  <button id="getQuote">Get Quote</button>
  </div>
</body>
Andy
  • 275
  • 2
  • 14
  • any console errors? – madalinivascu Jul 12 '16 at 05:20
  • Your code sample is not clear. The js should wrap in a 'script' tag. And you shoul put it in a function on document ready, to be able to bind on dom elements. – Mario Santini Jul 12 '16 at 05:21
  • your javascript is running before the button is there in html, you should try putting that below the html loading so that jquery can bind to that event – Sumit Jul 12 '16 at 05:22
  • @MarioAlexandroSantini sorry this was not more clear but JS was in separate file from HTML. Just pasted the relevant parts for brevity. – Andy Jul 12 '16 at 05:32

2 Answers2

2

Prevent the default click event, remove the parsing of the data:

$(function(){
    $('#getQuote').click(function (e){
            e.preventDefault();
            $.ajax({
              headers: {
                'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Accept': 'application/json'
              },
              method:'POST',
              dataType: 'json',
              url: 'https://andruxnet-random-famous-quotes.p.mashape.com/',
              success: function(response) {
                var ape = response//remove the parsing
                var quoteText = ape.quote;
                var quoteAuthor = ape.author;
                $(".quote").html(quoteText);
                $(".author").html(quoteAuthor);}
            });
          });
});

https://jsfiddle.net/cyLyn8ba/

madalinivascu
  • 32,064
  • 4
  • 39
  • 55
  • 1
    This worked, now just wish I knew more about why. Need to read the parsing docs. Many thanks. – Andy Jul 12 '16 at 05:30
  • If anyone comes across this, [this question](http://stackoverflow.com/questions/9111184/why-is-jquery-parsejson-not-necessary) explains the JSON parsing and why including this returns null. – Andy Jul 12 '16 at 05:45
1

jquery is intelligent enough to parse json response by itself, so you need to remove parsing function and everything should work fine :)

$('#getQuote').click(function (){
    $.ajax({
      headers: {
        'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      method:'POST',
      dataType: 'json',
      url: 'https://andruxnet-random-famous-quotes.p.mashape.com/',
      success: function(response) {
        var ape = response;
        var quoteText = ape.quote;
        var quoteAuthor = ape.author;
        $(".quote").html(quoteText);
        $(".author").html(quoteAuthor);}
    });
  });

codepen example

Amir Hoseinian
  • 2,312
  • 2
  • 24
  • 27