phpmailer导致托管上的跨源请求被阻止

时间:2014-12-23 13:39:54

标签: php ajax phpmailer

我是新手移动开发者,我正在使用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();

&GT;

Ajax(使用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);

        }

    });

};

我已经尝试过localhost并且运行良好。 但是当我尝试托管时,我有错误(在顶部)。

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

有什么错误吗?还是我想念的方法?

  

仅供参考:

     
      
  1. 我已经检查了我的托管上的SMTP(phpmailer)支持,并且支持得很好。

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

  4.   
  5. 我也尝试过:How to specify the SMTP server in PHPMailer?

  6.         

    但仍然没有机会。

谢谢, 任何帮助将是欣赏:))


-Edit: 在我的firebug(控制台)上没有我得到的错误,我已经激活$mail->SMTPDebug = 2;,但我看不到错误或其他东西,响应只是空的(没有任何) 我刚看到

  

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


-Edit 2: 通过请求@synchro关于导致错误的URL,下面是完整的错误:

  

阻止跨源请求:同源策略禁止读取   http:// myhost / www / myphp上的远程资源。这可以修复   将资源移动到同一域或启用CORS。

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

2 个答案:

答案 0 :(得分:0)

尝试在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");

答案 1 :(得分:0)

如果您从http://localhost/index.php投放您的网页并与http://somewhereelse/index.php对话,那就是因为域名不匹配而导致的access-control。您可以通过多种方式解决这个问题:

  • 设置其他人描述的{{1}}标题
  • 在您自己的服务器上代理请求,因此它不会与浏览器交叉显示。
  • 执行目标脚本在您自己的服务器上执行的任何操作

最后一个是最简单的。

此外,您似乎正在使用旧版本的PHPMailer,并将您的代码基于旧示例。获取最新的here,然后查看示例和wiki。