cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.4 and CORS

SOLVED

Magento 2.4 and CORS

hello, I am using magento 2.4, the application must call a webservice with XMLHTTPRequest, with javascript, the error that appears is: Access to XMLHttpRequest at '***' from origin '***' has been blocked by CORS policy : Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

 


So I looked on the forums, I modified the file:
/etc/apache2/apache2.conf
In order to add:
<Directory / var / www / html>
Order Allow, Deny
Allow from all
AllowOverride all
Header set Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "GET, POST, OPTIONS, DELETE"
</Directory>

 

unfortunately that does not solve my concern, would you have any leads I could use please?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.4 and CORS

It's good, 👍 I solved my problem 🙂

View solution in original post

9 REPLIES 9

Re: Magento 2.4 and CORS

Hello @alexandred8025 

 

I guess you can resolve this issue by adding this in your .htaccess :

Header add Access-Control-Allow-Origin "b.com"

This will allow the resources to load on the second domain. If you would prefer to allow the resources to load on all domains you can use :

Header add Access-Control-Allow-Origin "*" 

 

Hope it works !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Magento 2.4 and CORS

Thanks but it doesn't work, I put it in the .htaccess in:
/var/www/html/.htaccess
The error remains the same in the console.
Do you have another lead please?

Re: Magento 2.4 and CORS

@alexandred8025 

 

The .htaccess file is in root folder of your Magento Project, just change in :
var/www/html/MAGENTO_PROJECT/.htaccess

 

Let me know if it works ! Smiley Happy

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Magento 2.4 and CORS

Yes, it's root folder in var/www/html Smiley Very Happy

Doesn't work for me in .htaccess

Hmmmm, I don't know why, I've been on it for 3 days

Re: Magento 2.4 and CORS

curl -X OPTIONS http://www.****.com -i

 

HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.1
Date: Sun, 11 Oct 2020 17:01:13 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Location: http://infomallorca.es

Re: Magento 2.4 and CORS

It's good, 👍 I solved my problem 🙂

Re: Magento 2.4 and CORS

@alexandred8025 

 

Please share how you resolved and mark that solution so someone may get help from it Smiley Happy

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Magento 2.4 and CORS

PLease help me i am also facing the same issue