contact-form icon indicating copy to clipboard operation
contact-form copied to clipboard

Setting CORS headers

Open fthues opened this issue 5 years ago • 3 comments

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!

fthues avatar Dec 18 '19 12:12 fthues

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

ignism avatar Sep 16 '20 17:09 ignism

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.

steveDL avatar Oct 09 '20 12:10 steveDL

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.

danbrellis avatar Feb 24 '23 20:02 danbrellis