简体   繁体   中英

Angular-PHP CORS request blocked

I am trying to send from my angular project an email via a php service at the time a form is filled out.

When I try to send the request in the console I get the following error "Access to XMLHttpRequest at 'https://website/assets/php/mail.php?con_name=user&con_email=mail&con_message=test&con_subject=Hello%20World' from origin 'https://website' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

As advised by many of you within the PHP file I have already unsuccessfully included "header('Access-Control-Allow-Origin: *');" and "header('Access-Control-Allow-Methods: POST, GET, OPTIONS');"

The angular call is:

sendMail() {
    
    this.mailValidator()
    if(this.validate() && this.mailValidator() )
    {
      this.http.get("https://website.com/assets/php/mail.php?con_name="+this.name+"&con_email="+this.email+"&con_message="+this.message+"&con_subject="+this.object).subscribe((val) => {
      
      })
      this.dialogRef.close();
    }
 
  } 

The PHP code is

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
echo("Test")

?>

Could you try adding allow the Access-Control-Allow-Headers: Origin and see if that helps you?

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

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