Here's the ajax function I'm working with:
<div class="quiz-box"></div>
...
const url = window.location.href
const quizBox = document.getElementById('quiz-box')
let data
$.ajax({
type: 'GET',
url: `${url}data/`,
success: function(response){
// console.log(response)
data = response.data
data.forEach(el => {
for (const [question, answers] of Object.entries(el)){
quizBox.innerHTML = `
<b>${question}</b>
`
}
});
},
error: function(error){
console.log(error)
},
})
This ajax function fetches data from a Django view.
However, running this code results in the following error in the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') at Object.success at j
at Object.fireWith [as resolveWith]
at x
at XMLHttpRequest.b
What is the cause of this error and how can I fix it?