On submitting a form, I’m making a post request using ajax in custom code:
var Webflow = Webflow || [];
Webflow.push(function () {
$('#wf-form-myform').submit(function (e) {
e.preventDefault();
const $form = $(this); // The submitted form
$.ajax({
url: $form.attr('action'),
data: $form.serialize(),
method: $form.attr('method'),
contentType: "application/json",
dataType: "json",
})
.done((res) => { console.log('Success') })
.fail((res) => { console.log('Fail') })
.always(() => { console.log('Completed') });
});
});
This results in 2 preflight requests and a POST request (shown in image).
The requests with OPTIONS and POST methods succeed, but there is a 3rd request which fails. This subsequently causes the entire API call to end up in a failed state. I don’t understand where the second preflight request comes from and why does it fail despite setting liberal CORS headers. Has someone faced this before?
Extra info (headers of the 3 requests:)
- Successful OPTIONS request:
- Failed preflight request:
- Successful POST request:
Here is my site Read-Only: LINK