将数据从一个域发布到另一个域时获取Access-Control-Allow-Origin错误

时间:2016-08-19 18:05:36

标签: php jquery

最近我尝试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"];
?>

但是,当我在发送电子邮件时使用它时,我再次收到Access-Control-Allow-Origin错误。这是整个代码:

<?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错误。我哪里出错了?

1 个答案:

答案 0 :(得分:0)

如果您在请求中使用密码,则还应添加:'Access-Control-Allow-Credentials: true'标题行