craftcms / contact-form

Add a simple contact form to your Craft CMS site.
https://plugins.craftcms.com/contact-form
MIT License
294 stars 93 forks source link

Setting CORS headers #182

Open fthues opened 4 years ago

fthues commented 4 years ago

We're submitting data to the plugin from a different domain. We're setting the required CORS headers via .htaccess (so the OPTIONS request goes through) but Craft seems to remove them from the actual POST request, which seems to be a problem for Axios.

The way I see it there is no way of overriding the controller to add headers to the response. Any hints as to how to approach this?

Thanks!

ignism commented 4 years ago

Did you ever solve this? I’m running into similar problems.

steveDL commented 4 years ago

I have a similar issue POSTing to http://myurl.com/actions/contact-form/send. CURL returns a bunch of HTML and errors one starting with yii\web\BadRequestHttpException Post request required in /backend/vendor/craftcms/cms/src/web/Controller.php:356

But I guess this is result of not passing it what it needs as its a bad request. I trying to compose a valid request.

danbrellis commented 1 year ago

I know it's an old thread, but for anyone else with the same problem, one way I just solved dealing with the necessary headers for POST requests is to add the header in a custom module:

//modules/Module.php
Craft::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', 'http://localhost:2222');

If that's too open for you, I'm sure you could add conditionals to check the type of request, the action, etc.