为什么标头允许跨域不允许使用回显打印变量值


Why header allow cross domain does not allow use echo to print the variable value?

我有一个角度文件,它会发出一个后请求,将变量(电子邮件,用户,keysuser)发送到php文件以发送电子邮件。但是当我使用 echo 打印值时,响应是一个错误。让我向您展示:

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials: true');
$data = file_get_contents("php://input");
$dataJsonDecode  = json_decode($data);
$email = $dataJsonDecode->email;
$username = $dataJsonDecode->username;
$id = $dataJsonDecode->keyuser;
$subject = "Welcome";
$message="Hi".echo $username ." welcome to the site... blabla";
$header .= 'MIME-Version: 1.0' . "'r'n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$header .= 'From: GoVir <govir@example.com>' . "'r'n";
if(mail($email,$subject,$message,$header)){
 echo 'success';
}else{
echo 'Error';
}
?>

而这个错误

  • MLHttpRequest 无法加载 http://www.govircarpool.com/correo/correo.php。请求的资源上不存在"访问控制允许源"标头。因此,不允许访问源"http://localhost:8100"。响应的 HTTP 状态代码为 500。

当我只发送字符串或 html 时,它可以很好地发送电子邮件,但是当使用 echo 连接值时,发生了这些错误。

首先删除$message之前使用的回声

$message="Hi".$username ." welcome to the site... blabla";

并检查您的邮件功能是否返回为真。如果邮件函数不返回 true,那么您将始终得到"错误"。