First of all, $.getJSON and $.ajax are basically the same thing (by my understanding). It’s just that $.getJSON makes certain assumptions. It’s like going into a Chinese restaurant. If the #4 special gives you everything you want, you can just order that and it’s easy. But if you want to make 5 substitutions and add a few things and subtract others - they may tell you to get lost. $.getJSON is like a Chinese restaurant that is great if you what you want is simple and straightforward. If you need anything more complicated, you need to go next door to $.ajax. But if you look in back, you see that they are the exact same kitchen, cooks, and ingredients. They just have different attitudes about what you can order.
Sometimes that doesn’t matter because you can pass all the info you need in url string. I could not see how to pass the key in the url string. The web site offers no hints. And I didn’t know how to pass request headers in a $.getJSON so I used the more flexible and accommodating version, $.ajax. It does the same thing, it just looks different.
Since you insist and I was curious, I did more research and found that you can send request headers in a .getJSON, and you can do it thusly:
var API_KEY = "your-api-key-goes-here";
var url = "https://api.icndb.com/jokes/random";
$.getJSON(url, {"mashape-key": API_KEY }, function(json){
console.log(json);
});
and with a little tinkering I also got it to work with just the url:
var API_KEY = "your-api-key-goes-here";
var url = "https://api.icndb.com/jokes/random?mashape-key=" + API_KEY;
$.getJSON(url, function(json){
console.log(json);
});
If you want that “movies” string to work, you need to change the query a little to:
https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=10&mashape-key=tzXlShQE58mshigJbFTRFpPh7rNgp1QaECVjsn3RL072oIqvWA
Note that it is mashape-key and not X-Mashape-Key.
Let me know if that doesn’t work.
With your API key, that is:
var url = "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=10&mashape-key=tzXlShQE58mshigJbFTRFpPh7rNgp1QaECVjsn3RL072oIqvWA";
$.getJSON(url, function(json){
console.log(json);
});