简体   繁体   中英

Angular 8 with php and mysql database Cross-Origin Request Blocked:

I am trying to build a login and register with angular 8 platform, with php and mysql database.

php and mysql uses localhost/ and angular 8 uses localhost:4200 .

I am trying to send data to mysql database but there is a error message in the console:

在此处输入图像描述

Cross-Origin Request Blocked:

To communicate between localhost/ and localhost:4200 i used this code

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule ^([^\.]+)$ $1.php [NC,L]    

#Set the headers for the restful api 
Header always set Access-Control-Allow-Origin http://localhost:4200   
Header always set Access-Control-Max-Age "1000"   
Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin,    Authorization,    
Header always set Access-Control-Allow-Origin "*"   
Header always set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin"  
Header always set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS" 

but still I am getting this error. Is there anybody to help me?

From enable cors in .htaccess (this question is a duplicate.) You need to use add instead of set: Remove all the rules and replace by:

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 asterisk should be replaced by the correct domain in production mode.

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