// Submit feedback
async function submitFeedback() {
const email = document.getElementById('feedback-email').value.trim();
const text = document.getElementById('feedback-text').value.trim();
const captchaValue = document.getElementById('captchaValue').value.trim();
// Validate input
if (!email || !text || !captchaValue) {
alert("Email, content, and captcha cannot be empty!");
return;
}
const feedbackData = {
email,
text,
captcha_id: captchaId,
captcha_value: captchaValue
};
try {
// Generate signature
const signature = await generateHMAC(new URLSearchParams(feedbackData), key);
// Send request
const response = await fetch('https://127.0.0.1:8080/gongdan', {
method: 'POST',
credentials: 'include', // Ensure sending and receiving cookies
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Timestamp': Math.floor(Date.now() / 1000).toString(), // Current timestamp
'X-Signature': signature, // Pass the signature
'X-Nonce': Math.random().toString(36).substring(2, 15) // Randomly generate a nonce
},
body: new URLSearchParams(feedbackData)
});
// Handle response
const data = await response.json();
if (data.code === "0") {
document.getElementById('feedback-response').innerHTML = `<p>Feedback ticket created successfully!</p>`;
} else {
document.getElementById('feedback-response').innerHTML = `<p class="error">Feedback failed: ${data.data}</p>`;
}
} catch (error) {
document.getElementById('feedback-response').innerHTML = `<p class="error">Request failed: ${error.message}</p>`;
}
}
The problem with the code:
There is an issue when I execute it: after the browser sends an OPTIONS request, the following function calls to other backend APIs will also send an OPTIONS request. This keeps happening until I stop and restart the backend, even though the backend shows that the OPTIONS request has passed.
This is the Backend Console message:
[GIN] 2025/01/18 – 11:08:18 | 204 | 0s | 127.0.0.1 | OPTIONS “/captcha”
Session ID:
2025-01-18 11:08:18.4210575 +0800 CST m=+10.512820201 1m0s
1 2
[GIN] 2025/01/18 – 11:08:18 | 200 | 4.1433ms | 127.0.0.1 | GET “/captcha”
[GIN] 2025/01/18 – 11:08:23 | 204 | 0s | 127.0.0.1 | OPTIONS “/gongdan”
It is evident that the backend only received the OPTIONS request and did not receive the POST request. After that, no matter which API is called, only an OPTIONS request will be sent, and the subsequent requests will not be sent.
What should I do?