简体   繁体   中英

React has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header

According to this answers enable cors in .htaccess I would like to allow cross-origin requests from React application to the local server with Laravel application. So I added these lines at the beginning of my public/.htaccess. But it does not work.

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

The error says:

Access to XMLHttpRequest at 'http://localhost:8000/api/page/dynamic/111170/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Can somebody tell me please what am I doing wrong?

In your server you need to enable requests from the local host:3000 origin. By default most server frameworks will block requests that aren't from its own ip. This is called a cross origin request policy and its to help secure your application.

You'll need to change your servers CORS policy to include localhost:3000 - you'll need to google how to do this for the specific server framework you've chosen.

To reiterate, this is not a problem with your React code but with your server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM