从一个域到另一个域发布数据时,得到Access-Control-Allow-Origin错误


Getting Access-Control-Allow-Origin error while posting data from one domain to other

最近我尝试header('Access-Control-Allow-Origin: *');发布到其他域,它工作得很好。这是整个代码,仍然运行良好。

<?php
 header('Access-Control-Allow-Origin: *');
header("X-XSS-Protection: 0");
echo $_POST["roll"];
echo $_POST["id"];
echo $_POST["email"];
?>

然而,当我在发送电子邮件中使用它时,我再次获得访问控制允许起源错误。这是整个代码:

<?php
 header('Access-Control-Allow-Origin: *');
header("X-XSS-Protection: 0");
echo $_POST["roll"];
echo $_POST["id"];
echo $_POST["email"];
$subject = 'Reset Password - ITM Exodia';
$toEmail = '$_POST["email"]';
$message = 'Reset Password - ITM Exodia';          
$headers  = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$headers .= "From: ITM Exodia" . "'r'n";

$to=$toEmail;
$subject=$subject;
$from="Exodia@zarainforise.com"; 
$headers = "MIME-Version: 1.0'n";
$headers .= "Content-type: text/html; charset=iso-8859-1'n";
$headers .= "From: <".$from.">'n";
$headers .= "X-Priority: 1'n";
$message='<a href="http://localhost/exodia/reset.php?q=$_POST['id']" >Reset Password</a>';
$message .= "<br/>Regards $_POST['id']<br />saurav";

if (mail($to, $subject, $message, $headers )) {
  $data['msg']="Message send successfully";
} 
else {
  $data['msg']="Please try again, Message could not be sent!";
}  
?>

使用这个代码,数据甚至没有发布,它给出了Access-Control-Allow-Origin错误。我哪里做错了?

如果您在请求中使用密码,您还应该添加:'Access-Control-Allow-Credentials: true' header line