In AnQiCMS front-end, elegantly obtain specific error prompt information after verification code verification fails
As a senior website operations expert, I know the importance of user experience in website operations.When users operate on your AnQiCMS website (such as leaving messages, comments, registration, etc.), if they encounter a failed captcha verification, clear and immediate error messages can greatly reduce users' frustration and guide them to correctly complete the operation.How can one accurately obtain and display the specific error prompts after the verification failure of these captcha verification codes in the front-end of AnQiCMS?Today, let's delve into it in depth.
Understand the AnQiCMS verification code mechanism
Firstly, let us briefly review the regular workflow of AnQiCMS frontend captcha. According to the documentation provided,tag-/anqiapi-other/167.htmlEnglish, we can see that AnQiCMS usually interacts with the backend API through front-end JavaScript to obtain and display the captcha.
When your web page is loaded, or the user clicks to refresh the captcha, the front-end will send a request to the AnQiCMS backend, usuallyGET /api/captcha. The backend will generate a response containingcaptcha_id[Captcha Unique Identifier] andcaptchaThe JSON data of (Captcha image Base64 encoding or image URL). After the frontend JavaScript receives these data, it willcaptcha_idStored in a hidden field, andcaptchadisplayed to the user.
This stage, we mainly deal with the display of captcha. And our focus today is on the user filling in the captcha and submitting the form.When the verification fails.How to get specific prompts.
Error message after verification code acquisition fails
The actual verification of the captcha occurs when the user submits the form to the backend. When the user fills in the captcha and clicks the submit button, the frontend will send the form data (including the captcha content)captchaand the captcha identifiercaptcha_id) sent to the backend for processing. If the backend finds a mismatch or other errors during the captcha verification, it needs to return these specific error messages to the frontend.
AnQiCMS in handling such form submissions usually provides a flexible response mechanism.tag-/anqiapi-other/158.htmlandtag-/anqiapi-other/162.htmlIn the documents related to form submissions, we see a key parameter -return。Through setting parameters when submitting the formreturnWe can control the data format returned by the backend:
return=html(Default):If not setreturnor set tohtmlThe backend usually returns a complete HTML page, which may be the current page with error messages or redirected to an error message page.In this case, the front-end needs to parse the entire HTML page to extract error information, which is usually complex and provides a poor user experience.return=json(recommended)English for: In order to facilitate front-end JavaScript processing, we strongly recommend setting when submitting the formreturn=json. So even if the verification fails, the backend will return a structured JSON object that includescode(error code),msg(specific error information) fields.
Therefore, the core of obtaining the specific prompt information after the verification code fails is:Use asynchronous requests (AJAX) to submit the form and explicitly require the backend to return JSON formatted error information.
Implementation steps
- Prevent the default form submission behavior:Using JavaScript to intercept form submissions.
submitEvents, preventing the browser from performing traditional page jump submission. - Building asynchronous requests:Pass
fetchAPI or jQuery's$.ajaxMethods, manually constructing an HTTPPOSTRequest, send form data to the backend. - including
return=jsonParameters:In the request'sbodyorURLbe sure to includereturn=jsonParameter. - Parse the backend JSON response:After receiving the JSON data returned by the backend, check the
codeField to determine whether the operation is successful, andmsgField to obtain specific error提示 information. - Front-end error display:The obtained
msgInformation is displayed dynamically to the user, for example, showing red text prompts below the captcha input box, or through pop-up prompts.
Sample code (for example, a comment form)
Assuming you have already integrated the captcha according to the instructions in the comment formtag-/anqiapi-other/167.htmland the form HTML structure is similar totag-/anqiapi-other/162.html:
`html
// === 验证码图片获取逻辑(来自tag-/anqiapi-other/167.html) ===
const captchaImage = document.getElementById('get-captcha');
const captchaIdField = document.getElementById('captcha_id');
const captchaErrorSpan = document.getElementById('captchaError');
function fetchNewCaptcha() {
fetch('/api/captcha')
.then(response => response.json())
.then(res => {
if (res.code === 0 && res.data) {
captchaIdField.value = res.data.captcha_id;
captchaImage.src = res.data.captcha;
captchaErrorSpan.style.display = 'none'; // 隐藏之前的错误信息
} else {
// 如果获取验证码图片本身失败
captchaErrorSpan.textContent = res.msg || '获取验证码失败,请刷新页面。';
captchaErrorSpan.style.display = 'block';
}
})
.catch(err => {
console.error('获取验证码图片时发生网络错误:', err);
captchaErrorSpan.textContent = '网络错误,无法获取验证码。';
captchaErrorSpan.style.display = 'block';
});
}
captchaImage.addEventListener("click", fetchNewCaptcha);
document.addEventListener("DOMContentLoaded", fetchNewCaptcha); // 页面加载时自动获取一次
// === 表单提交及错误信息处理逻辑 ===