7357963110
May 30, 2023, updated at 11:10:02 (UTC)
1351
4
0
Error 500 Ajax
Hi,
ich verwende in meinem Code ein Script, welches eine HTTP Anfrage an den serverseitigen Punkt sendet. Vom Grunde her findet der Server alles, was er braucht. Er gibt mir nur einen Error 500 zurück. Chrome DevTools gibt mir den Fehler bei
"xhr.send();"
Was könnte in diesem Fall das Problem sein?
hier der Code:
ich verwende in meinem Code ein Script, welches eine HTTP Anfrage an den serverseitigen Punkt sendet. Vom Grunde her findet der Server alles, was er braucht. Er gibt mir nur einen Error 500 zurück. Chrome DevTools gibt mir den Fehler bei
"xhr.send();"
Was könnte in diesem Fall das Problem sein?
hier der Code:
<script>
function getQuizData() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'get_questions.php', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var quizData = JSON.parse(xhr.responseText);
displayQuiz(quizData);
}
};
xhr.send();
}
function displayQuiz(quizData) {
var quizContainer = document.getElementById('quiz-container');
var randomQuestions = shuffleArray(quizData.questions).slice(0, 30);
randomQuestions.forEach(function (question) {
var questionElement = document.createElement('div');
questionElement.innerHTML = '<h3>' + question.question + '</h3>';
question.choices.forEach(function (choice) {
var choiceElement = document.createElement('input');
choiceElement.type = 'radio';
choiceElement.name = question.id;
choiceElement.value = choice;
questionElement.appendChild(choiceElement);
var labelElement = document.createElement('label');
labelElement.innerHTML = choice;
questionElement.appendChild(labelElement);
questionElement.appendChild(document.createElement('br'));
});
questionElement.addEventListener('change', function (event) {
var selectedAnswer = event.target.value;
if (selectedAnswer === question.correctAnswer) {
alert('Richtige Antwort!');
} else {
alert('Falsche Antwort!');
}
});
quizContainer.appendChild(questionElement);
quizContainer.appendChild(document.createElement('hr'));
});
}
function shuffleArray(array) {
var currentIndex = array.length,
temporaryValue,
randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
getQuizData();
</script>
Please also mark the comments that contributed to the solution of the article
Content-Key: 7360051655
Url: https://administrator.de/contentid/7360051655
Printed on: May 4, 2024 at 08:05 o'clock
4 Comments
Latest comment