Fix CORS error
This commit is contained in:
40
display.js
40
display.js
@@ -85,42 +85,22 @@ newQuestionBtn.addEventListener('click', async () => {
|
|||||||
.map(cb => cb.value);
|
.map(cb => cb.value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Build the request URL with CORS proxy
|
// Just fetch a random question without filtering
|
||||||
let requestUrl = CORS_PROXY + encodeURIComponent(API_URL);
|
const response = await fetch(API_URL, {
|
||||||
|
method: 'GET' // Changed to GET - simpler
|
||||||
// For POST requests with categories, we need a different approach
|
});
|
||||||
let fetchOptions = {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// If categories are selected, use POST through proxy
|
|
||||||
if (categories.length > 0) {
|
|
||||||
// Some CORS proxies don't handle POST well, so we'll use GET for now
|
|
||||||
// and filter on the client side if needed
|
|
||||||
console.log('Selected categories:', categories);
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(requestUrl, fetchOptions);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error('API request failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
// If categories were selected, check if question matches
|
// The API returns the question directly, not wrapped
|
||||||
// If not, fetch another one (simple client-side filtering)
|
|
||||||
if (categories.length > 0 && !categories.includes(data.category)) {
|
|
||||||
console.log('Question category mismatch, fetching another...');
|
|
||||||
newQuestionBtn.click(); // Try again
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentQuestion = data;
|
currentQuestion = data;
|
||||||
|
|
||||||
|
console.log('Fetched question:', currentQuestion);
|
||||||
|
|
||||||
questionText.textContent = currentQuestion.tossup_question;
|
questionText.textContent = currentQuestion.tossup_question;
|
||||||
questionCategory.textContent = currentQuestion.category;
|
questionCategory.textContent = currentQuestion.category;
|
||||||
correctAnswer.textContent = currentQuestion.tossup_answer;
|
correctAnswer.textContent = currentQuestion.tossup_answer;
|
||||||
@@ -148,11 +128,11 @@ newQuestionBtn.addEventListener('click', async () => {
|
|||||||
readQuestionBtn.disabled = false;
|
readQuestionBtn.disabled = false;
|
||||||
currentQuestionNumber++;
|
currentQuestionNumber++;
|
||||||
|
|
||||||
console.log('Question loaded successfully:', currentQuestion);
|
console.log('Question loaded successfully!');
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching question:', error);
|
console.error('Error fetching question:', error);
|
||||||
alert('Error loading question. The CORS proxy might be down. Try again in a moment.');
|
alert('Error loading question. The API might be down. Check console for details.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user