Kingston_Pendrive/Suli/12.b/Webprogramozás/Gyakorlat/Dolgozat/2023. 12. 12 - Ajax lekérések/script.js
2024-11-19 19:04:02 +01:00

34 lines
1.1 KiB
JavaScript

buttons = document.querySelectorAll("button")
buttons[0].addEventListener("click", ()=>{
const response = document.querySelector(".response")
response.innerHTML = ""
const xhttp = new XMLHttpRequest()
xhttp.onload = function() {
const jsonfile = JSON.parse(this.responseText)
response.innerHTML = `${jsonfile["setup"]}<br>`
response.innerHTML += jsonfile["delivery"]
}
xhttp.open("GET", "https://v2.jokeapi.dev/joke/Any?type=twopart", true)
xhttp.send()
})
buttons[1].addEventListener("click", ()=>{
const response = document.querySelector(".response")
response.innerHTML = ""
let responsetext = ""
const xhttp = new XMLHttpRequest()
xhttp.onload = function() {
const jsonfile = JSON.parse(this.responseText)
for (let i of jsonfile.jokes) {
responsetext += `${i["setup"]}<br>`
responsetext += `${i["delivery"]}`
}
response.innerHTML = responsetext
}
xhttp.open("GET", "https://v2.jokeapi.dev/joke/Any?type=twopart&amount=10", true)
xhttp.send()
})