Help with my logic

Help with my logic
0

#1

Hello I am having some problems with using ajax to get the json data.

I am using the https://market.mashape.com/andruxnet/random-famous-quotes api to get the random quotes but my code does not seem to be working. When I press the button there is no response. I do not get an error message, nothing is written to the console, and there is no error alert. I’m not sure what I have done wrong at this point

here is my JS

$(document).ready(function() {

$.ajax({
  url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous",
  beforeSend: function(xhr) {
    xhr.sendRequestHeader("X-Mashape-Key", "jW0FyGBFqymshTXPHcdNJY0Jqczpp1NinCyjsnaRYZwqITNUwO");
    xhr.sendRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.sendRequestHeader("Accept", "application/json");
  },
  dataType: "json",
  type: "POST",
  success: function(data) {
    var quoteData = data;
    var quoteText = quoteData["quote"];
    var quoteAuthor = quoteData["author"];

    $("#quote").html(quoteText);
    $("#author").html(quoteAuthor);
    
    console.log(data);
    
  },
  error: function(err) { alert(err); }
});

});


#2

I can’t say this is for sure the problem but what is all of the beforeSend code doing? I personally just used $.getJSON(url, function) so I can’t say for sure what the problem is but that would be my first guess…definitely seems weird that the console isn’t writing anything.


#3

You have an error ! Instead of alert(err) use console.log to have more details. It seems like you don’t have an API key for theses requests :

“Missing Mashape application key. Go to http://docs.mashape.com/api-keys to learn how to get your API application key.”


#4

Hello I think I found my problem. I was supposed to use setRequestHeader instead of sendRequestHeader. Also switching to console.log helped me look for the error thank you very much. Also the reason I was doing it this way was because that was how Mashape described implementing the api into javascript. Here is the link to it http://docs.mashape.com/javascript.

Thanks for all of your help!