phpmailer 导致跨源请求在主机上被阻止


phpmailer causing Cross-Origin Request Blocked on Hosting

我是新手移动开发人员,我正在使用phonegap作为我的框架,我也使用firebug来简化我寻找错误/错误。

我收到此错误(我在火虫上遇到(:

跨源请求被阻止:同源策略不允许.....

这是我的代码(在服务器端,因为罪魁祸首可能是phpmailer和ajax(:

PHPMailer :

<?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();

?>

Ajax (using jquery(

function sendForgotPassword(FPEmail) { 
    var requestor = FPEmail;
    //create form_data for post data on ajax PHP
    var form_data = new FormData();
    form_data.append("PHPRequestor", requestor);
    $.ajax ({
        type: "POST",
        url: to_phpSide,
        data: form_data,
        contentType : false,
        processData : false,
        beforeSend: function() {
            loadingPageW(1);
        },
        success: function(data){
            if (data == 'success') {
                //when success
            } else if (data == 'no email') {
                //when no email 
            } else {
                //when error or something else occured
            }
        }, //for error message
        error: function (xhr, errorStats, errorMsg) {
            alert("error: "+xhr.errorStats+" , "+errorMsg);

        },
        complete: function() {
            loadingPageW(2);
        }
    });

};

我已经在本地主机中尝试过并且工作得很好。但是当我尝试托管时,我遇到了错误(顶部(。

我真的很好奇,如果由于我的页面试图请求跨域内容而导致的错误,那么为什么在其他页面上(我使用相同的方法,但没有 phpmailer(在托管方面效果很好?

有错误吗?还是我错过的方法?

仅供参考:

  1. 我已经检查了我的主机上的SMTP(phpmailer(支持,这很好。

  2. 我没有将html页面放在托管中,我只是将.php放在托管中

  3. 我也已经尝试了:如何在PHPMailer中指定SMTP服务器?

但仍然没有机会。

谢谢任何帮助将不胜感激:)


-编辑:在我的Firebug(控制台(上,我没有收到任何错误,我已经激活了$mail->SMTPDebug = 2;,但是我看不到错误或其他内容,响应只是空的(那里什么都没有(我只是看到

跨源请求被阻止:同源策略不允许.....


-编辑 2 :通过请求@synchro有关导致错误的URL的信息,以下是完整错误:

跨源请求被阻止:同源策略不允许读取 远程资源位于 http://myhost/www/myphp。这可以通过以下方式修复 将资源移动到同一域或启用 CORS。

注意:myhost是指我用于托管的页面。myphp 是包含上述代码(PHPMailer 代码(的.php文件。

如果您从http://localhost/index.php提供页面并且它与http://somewhereelse/index.php对话,那是跨域的,因为域不匹配。您可以通过多种方式解决此问题:

  • 按照其他人的描述设置access-control标头
  • 在您自己的服务器上代理请求,使其不会在浏览器中显示为跨源。
  • 执行
  • 目标脚本在您自己的服务器上执行的任何
  • 操作

最后一个是最简单的。

此外,看起来您使用的是旧版本的 PHPMailer,并且您的代码基于旧示例。在此处获取最新信息,并查看示例和维基。

尝试在 PHP 中使用以下代码:

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");