Reputation: 11
I am trying to get an API working which gets trivia questions from this site: https://opentdb.com/api_config.php. Here is my code:
if (auth && data.user == user && match('trivia', data.msg)) {
$("button").click(function() {
$.get("https://opentdb.com/api.php?amount=10&token=d6685dc31db69e33eeb1c3828ffa2c587d5ec43dc6dd995ebefe85681796d149", function(data, status) {
sock.chat("Data: " + data + "\nStatus: " + status);
});
});
}
It doesn't 'sock.chat' anything, I am new to this so sorry if there is an obvious mistake. I am using this script on Tampermonkey on a website if that helps.
Upvotes: 0
Views: 983
Reputation: 311
you can see detail of api response on below code.
$.get("https://opentdb.com/api.php?amount=10&token=d6685dc31db69e33eeb1c3828ffa2c587d5ec43dc6dd995ebefe85681796d149", function(data, status) {
for(i = 0 ; i <data.results.length; i++){
alert("category: " + data.results[i].category + "\n " +
"correct_answer: " + data.results[i].correct_answer + "\n " +
"difficulty: "+ data.results[i].difficulty + "\n " +
"incorrect_answers: " + data.results[i].incorrect_answers.length + "\n " +
"question: " + data.results[i].question + "\n " +
"type: " + data.results[i].type + "\n\n\n " +
"status: " + status);
}
});
you can manipulate response with dom element instead of "alert" function.
Upvotes: 0
Reputation: 5546
This code is working.
$.get("https://opentdb.com/api.php?amount=10&token=d6685dc31db69e33eeb1c3828ffa2c587d5ec43dc6dd995ebefe85681796d149", function(data, status) {
console.log(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 9812
Here is a working example:
const url = "https://opentdb.com/api.php?amount=10&token=d6685dc31db69e33eeb1c3828ffa2c587d5ec43dc6dd995ebefe85681796d149";
fetch(url)
.then(res => res.json())
.then(json => console.log(json.results))
.catch(error => console.error(error))
Upvotes: 2