0

What's the equivalent axios code for this:

<form method='post' action='https://sample.com' enctype='multipart/form-data'>

I tried the following

const bodyFormData = new FormData();
        bodyFormData.set('name', inputData.name);
        bodyFormData.set('hush_sender', inputData.email);
        bodyFormData.set('subject', inputData.subject);
        bodyFormData.set('message', inputData.message);

        const response = await axios({
            method: 'post',
            url: `https://sample.com`,
            data: bodyFormData,
            config: {
                headers: {
                    'Content-Type': 'multipart/form-data',
                    enctype: 'multipart/form-data'
                }
            }
        });

But I get the following error when using axios:

Access to XMLHttpRequest at 'https://sample.com' from origin 'http://localhost:3001' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

(I don't have control over mydomain.com. That is a third party api. They sent the code in the html form, but I want to use axios.)

Jessica
  • 9,379
  • 14
  • 65
  • 136
  • @Quentin So there's no way I can prevent the navigation to the new page? – Jessica Nov 12 '19 at 19:49
  • If you own sample.com then you can provide cors headers. You can also configure a [proxy](https://create-react-app.dev/docs/proxying-api-requests-in-development/) if you are using create-react-app – HMR Nov 13 '19 at 07:55
  • @HMR I don't own it... That's why I can't use cors.. I think – Jessica Nov 13 '19 at 19:33
  • If it's just for testing you can add a chrome plugin that wil allow CORS – HMR Nov 13 '19 at 21:43
  • @HMR I need this for production.... – Jessica Nov 13 '19 at 21:56
  • Then there is nothing you can do. CORS are enforced by the browser so other sites cannot just use your API's. If it wasn't that way then anyone can just use anyone's API's without permission. – HMR Nov 13 '19 at 21:58
  • Got it. Thank you @HMR !!! – Jessica Nov 13 '19 at 22:46

0 Answers0