Event.preventDefault is not working in JS for form submission
// js
document.addEventListener("DOMContentLoaded", function () {
const individualOrderForm = document.getElementById('individual-order-form');
async function sendAjax(form) {
try {
const response = await fetch(form.action, {
method: 'POST',
body: new FormData(form)
});
const data = await response.json();
if (data.status === "success") {
showToast(data.detail, "success");
disableForm(form);
} else {
let errors = JSON.parse(data.errors);
let errorsText = Object.entries(errors)
.map(([field, errors]) => {
return errors.map(error => `${error.message}`).join('\n');
})
.join('\n');
showToast(`${data.detail}\n${errorsText}`, "danger");
}
} catch (error) {
console.log(error);
showToast(gettext("Произошла ошибка. Повторите попытку позже."), "danger");
}
}
individualOrderForm.addEventListener(
'submit',
function (e) {
e.preventDefault();
sendAjax(individualOrderForm);
},
{passive: false}
);
}
);
<form id="individual-order-form"
action="{% url 'mainpage:individual-order-negotiate' %}"
method="POST"
class="d-flex w-100 flex-column justify-content-start align-items-start text-start card-body">
<fieldset class="w-100">
<legend class="text-center">
<h2 class="fs-4 fw-normal" id="contact-us">
{% translate 'Свяжитесь с нами' %}
</h2>
</legend>
<div class="form-text">{% translate 'Заполните форму, чтобы заказать букет из индивидуального состава.' %}</div>
{% csrf_token %}
<div class="mb-3 w-100">
<label for="customerNameInput" class="form-label">{% translate 'Имя' %}</label>
<input class="form-control text-start" id="customerNameInput" aria-describedby="nameHelp" name="first_name"
required>
</div>
<div class="mb-3">
<label for="contact-method-input" class="form-label">
{% translate 'Тел. номер в любой соц. сети или ссылка профиля' %}
</label>
<div class="d-flex">
{{ individual_order_form.contact_method }}
</div>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input checkbox-dark" id="recallCheckbox" name="recall_me">
<label class="form-check-label" for="recallCheckbox">
<small>
{% translate 'Разрешаю мне перезвонить' %}
</small>
</label>
</div>
<button class="btn btn-dark-green">{% translate 'Связаться' %}</button>
</fieldset>
</form>
Unfortunately, on iOS (at least on iOS), the following code does not work as expected:
individualOrderForm.addEventListener(
'submit',
function (e) {
e.preventDefault();
sendAjax(individualOrderForm);
},
{ passive: false }
);
When submitting the form via a POST request, the page is redirected to the URL where the POST request is sent, and I see the returned JSON object displayed on a black screen.
However, this works perfectly on both PC and Android devices.
Regrettably, I am unable to resolve this issue.
I came across this discussion, which seemed like a logical solution, but unfortunately, it did not resolve the problem:
GitHub Discussion: React Issue #13369.
I have already tried clearing the cache on both the client and server sides, running manage.py collectstatic, and restarting the server, but the issue persists.
Any assistance or guidance would be greatly appreciated.