I am trying to submit a service call request using Wix Forms. The request works perfectly on Postman, but it fails when using Wix Forms, showing a CORS policy error.
Backend Code (backend/formHandler.jsw):
import { fetch } from 'wix-fetch';
export function submitServiceCallBackend(formData) {
const url = "https://80.81.34.29:9554/api/ServiceCalls/ServiceCallAdd";
const headers = {
"Content-Type": "application/json"
};
const body = JSON.stringify(formData);
return fetch(url, {
method: 'POST',
headers: headers,
body: body
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.catch(error => {
console.error('Error:', error);
throw error;
});
}
Frontend Code:
import { submitServiceCallBackend } from 'backend/formHandler';
$w.onReady(function () { $w('#submitButton').onClick(() => { const formData = { Token: "TestServiceCall", LicTradNum: $w('#license').value, Subject: $w('#subject').value, Description: $w('#description').value, ContactPersonEmail: $w('#email').value, ContactPersonName: $w('#name').value, ContactPersonPhone: $w('#phone').value };
submitServiceCallBackend(formData)
.then(response => {
console.log('Success:', response);
})
.catch(error => {
console.error('Error:', error);
});
});
});
Error Messages:
Console:
Access to fetch at ‘https://editor.wix.com/html/editor/web/renderer/render/document/9723edf9-4a0c-4251-828a-bd598d31a532/_api/wix-forms/v1/submit-form’ from origin ‘https://b46f501b-56da-4e23-aeb9-a02a6b083b93.dev.wix-code.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: The ‘Access-Control-Allow-Origin’ header has a value ‘http://editor.wix.com’ that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
Wix console:
Network response was not ok Forbidden
What I’ve Tried:
- The API request works fine on Postman.
- The error occurs when using Wix Forms due to CORS policy issues.
Question:
How can I resolve this CORS policy issue and successfully submit the form data using Wix Forms? Any help or pointers would be greatly appreciated.
Thanks in advance!
eyal yehiely is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.