• Chris Baboujian
  • NEWBIE
  • 10 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
TLDR; Instead of native form submission for web2lead form, I am using REST API to hit the https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8 endpoint with form data in the request body to submit the Web2Lead form. And it's getting CORS error.
---
While the raw form looks like this
 
<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">


I have built the form using some custom components in React and providing the below `handleSubmit` handler to it to submit the form.
const actionUrl =
    'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

  const handleSubmit = async (formValues) => {
    try {
      await axios.post(actionUrl, formValues, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      });
    } catch (error) {
      console.error(error);
    }
  };

And this axios request is getting CORS error on the browser although I added the domain to the whitelist on the security settings.
Are Web2Lead forms intended to be submitted as a native form always? Is there a way to submit this form using axios without getting the CORS error?