phpmailer导致主机上的跨源请求被阻止
我是一名新手移动开发者,我使用phonegap作为我的框架,我还使用firebug来帮助我查找我所发现的错误/bug 我得到了这个错误(我在firebug上得到的): 已阻止跨来源请求:同一来源策略不允许 这是我的代码(在服务器端,因为可能罪魁祸首是phpmailer和ajax): PHPMailer:phpmailer导致主机上的跨源请求被阻止,php,ajax,phpmailer,Php,Ajax,Phpmailer,我是一名新手移动开发者,我使用phonegap作为我的框架,我还使用firebug来帮助我查找我所发现的错误/bug 我得到了这个错误(我在firebug上得到的): 已阻止跨来源请求:同一来源策略不允许 这是我的代码(在服务器端,因为可能罪魁祸首是phpmailer和ajax): PHPMailer: <?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: X-Reque
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: X-Requested-With");
header("Content-Type: text/html; charset=UTF-8");
include 'db_connect.php';
$applicantEmail = $_POST['PHPRequestor'];
$forgottenPass = '';
//for checking email
$q = "SELECT * FROM user WHERE email = '".$applicantEmail."'";
$result = mysqli_query($con, $q);
if ($result->num_rows == 0) {
echo "no email";
} else if ($result->num_rows == 1) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$forgottenPass = $row["pw"];
}
require_once ('class_email/PHPMailerAutoload.php'); //include library phpmailer using auto load
/*
require_once 'class_email/class.phpmailer.php';
require_once 'class_email/class.smtp.php';
*/
$mail = new PHPMailer();
$body =
"<body style='margin: 5px;'>
<br/>
<strong> 'Forgot Password' </strong> :
<br/>
<div style='width: 320px; border:#000000 solid 2px;'>
Your pass : <strong> ".$forgottenPass." </strong> <br/>
</div>
<br/>
Thanks
<br/>
</body>";
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); //Using SMTP
//Activated debug SMTP to see http response
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; //Authentication
$mail->SMTPSecure = "tls";
// sets the prefix to the
$mail->Host = "smtp.gmail.com"; //Set GMAIL as the SMTP
$mail->Port = 587; //Set the SMTP port for the GMAIL server
$mail->Username = "myemail@email.com"; //Email
$mail->Password = "thepassword"; //Password
//This will add 'Your Name' to the from address, so that the recipient will know the name of the person who sent the e-mail.
$mail->SetFrom("myemail@email.com", "Request Forgot Password");
$mail->AddReplyTo("myemail@email.com", "Request Forgot Password");
$mail->Subject = "Request Forgot Password', user : ".$applicantEmail;
$mail->MsgHTML($body);
$address = "targetemail@email.com"; //target email
$mail->AddAddress($address, "'Permintaan Lupa kata sandi'");
if($mail->Send()) {
echo "success";
} else {
echo "Oops, Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
$con->close();
})
我已经在localhost上试过了,效果很好。
但当我尝试托管时,我出现了错误(在顶部)
我真的很好奇,如果由于我的页面试图请求跨域的内容而导致错误,
那么,为什么在其他页面(我使用相同的方法,但没有phpmailer)上可以很好地进行托管呢
有什么错误吗?还是我错过的方法
供参考:
-编辑: 在我的firebug(控制台)上,我没有收到任何错误,我已经激活了
$mail->SMTPDebug=2代码>,但我看不到错误或其他内容,响应是空的(没有任何内容)
我明白了
已阻止跨来源请求:同一来源策略不允许
-编辑2:
通过请求@synchro了解导致错误的URL,以下是完整错误:
已阻止跨源请求:同一源策略不允许读取
远程资源位于http://myhost/www/myphp。这可以通过以下方式解决:
将资源移动到同一域或启用CORS
注:
myhost是指我用于托管的页面。
myphp是包含上述代码的.php文件(PHPMailer代码)请尝试在PHPMailer中使用此代码:
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");
如果您从http://localhost/index.php
并且它与http://somewhereelse/index.php
,这是交叉源代码,因为域不匹配。您可以通过以下几种方式解决此问题:
- 如其他人所述,设置
访问控制
标题
- 在您自己的服务器上代理请求,使其在浏览器中不会显示为跨原点
- 执行目标脚本在您自己的服务器上执行的任何操作
最后一个是最简单的
另外,看起来您使用的是旧版本的PHPMailer,并且代码基于旧示例。获取最新信息,查看示例和wiki。对不起,我只是想澄清一下。这段代码添加到我所有的PHPMailer类中了吗?(也叫“PHPMailerAutoload.php”)或者你是说,我发布的上面的代码编辑:我试过了,但还是没有机会。。感谢您的帮助:)删除前三行(以页眉开头),并将我在上面提供的代码放在那里。很抱歉,我仍然收到相同的错误,还有其他建议吗?:)这与PHPMailer无关。PHPMailer完全在服务器端运行,跨源错误在客户端。Firebug应该向您显示被拒绝请求的URL,该信息对于诊断您的问题至关重要。@Synchro感谢您指出此处发生的情况。:)因此,导致此错误的是我的客户端(跨源请求被阻止)?-查看我的URL更新,您需要诊断我的问题,如错误所示-将其访问的URL移动到您的域,它将不再是跨源的。感谢提供解决方案,但我仍然对您的解决方案有点困惑。您所说的“URL”是“http://myhost/www/myphp”?或者我使用的本地URL(因为我使用HTML(在localhost中)来访问宿主中的my.php)。我只是想知道,有没有什么解决方案可以在不移动URL的情况下克服这个错误(也没有JSONP)?无论如何谢谢@Synchro:)谢谢你给我的建议和解决方案:)我还在努力。。另外,我已经使用了最新版本的PHPMailer(我使用了您提供给我的链接)。多亏了你,我已经在这里找到了真正的罪魁祸首:)无论如何,谢谢。
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");