onreadystatechange readystate = 1但不是2,3或4

时间:2015-08-21 20:08:25

标签: javascript php html email

这是我在stackoverflow上的第一个求助请求,所以,请保持温和。

我已经搜索了stackoverflow以获取有关此问题的问题,但我们找不到任何可以解决我问题的方法来解决我的问题。

我尝试使用php从网页发送包含用户密码的电子邮件。我使用Mozilla / 5.0(Macintosh; Intel Mac OS X 10_8_5)AppleWebKit / 600.7.12(KHTML,如Gecko)版本/ 6.2.7 Safari / 537.85.16作为我的浏览器 - 但我需要它从任何主要的现代浏览器。

现在https://pie.gd/test/script-link-events/似乎表明我的浏览器不支持,任何 Mac浏览器都不支持,但这似乎与我的经验不符 - 没有我使用onreadystatechange时出错的浏览器控制台。

我使用的javascript代码是:

    function forgotEmail(str_code)
    {
        bool_debug = <?php echo $bool_debug ?>;

        if (bool_debug) { alert("php/forgotEmail.php?"+str_code+" is being called."); } 

        if (window.XMLHttpRequest) 
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } // if (window.XMLHttpRequest)
        else 
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } // if (window.XMLHttpRequest) else

        xmlhttp.onreadystatechange = function() 
        {
            alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)

            if (xmlhttp.readyState==4) 
            {
                alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)
                if (xmlhttp.status==200)
                {
                    alert("An email with your password has been sent to the account you gave with your CO Gold submission.  It may take a few minutes to arrive.  Please also check your spam folder.");
                } // if (xmlhttp.status==200)
            } // if ((xmlhttp.readyState==4) && (xmlhttp.status==200))
            else
            {
                alert("There was a problem sending the email with your password.  Please click 'Forgot Password' again.  If you get this alert a third time, please email...");
            } // if ((xmlhttp.readyState==4) && (xmlhttp.status==200)) else
        } // xmlhttp.onreadystatechange = function() 

        xmlhttp.open("GET", "php/forgotEmail.php?"+str_code);

    } // function forgotEmail(str_code)

并且AJAX调用的php页面中的相关代码是:

…
$headers = 'From: <a valid email address>'."\r\n";
// the message
$str_body = $row_psswd['NameFirst'].",\n\n"
    .'Your password is:  '.$row_psswd['psswd']."\n\n";

// use wordwrap() if lines are longer than 70 characters
$str_body = wordwrap($str_body,90);

// send email
$return = mail($row_psswd['Email'], "Forgotten Password", $str_body, $headers); 

echo str_replace("\n", "<br />\n", $str_body);

echo "<br />\n<br />\n";
echo (($return) ? 'email sent' :  'email not sent');

现在,当我手动调用php页面时,电子邮件发送没问题。

但是当我点击调用javascript的网页中的HTML按钮时,不会发送任何电子邮件。我知道php页面被正确调用,因为我得到了:

php/forgotEmail.php?code=JIxRIt is being called.

来自我在javascript代码中的第一个调试alert。但我只能从alert

中获得一个xmlhttp.onreadystatechange = function()

readyState = 1 status=0

然后&#34;东西没有工作&#34; alert

There was a problem sending the email with your password. Please click 'Forgot Password' again. If you get this alert a third time, please email…

所以看来readyState永远不会接受值2,3或4 - 不接受值4表示电子邮件没有被发送,对吧?

但事实是我得到了

readyState = 1 status=0

让我觉得onreadystatechange实际上是Safari支持的(对于Mac)。

我希望我已经清楚地说明了这个问题,并且有人能够帮助我解决这个问题,因为它让@ $ ^&amp; $%@感到沮丧。

1 个答案:

答案 0 :(得分:0)

您需要使用send()方法将您的请求发送到服务器

xmlhttp.send() 

另外,你应该移动你的“东西不起作用”代码,因为现在任何不是4的readyState都会触发它。

 xmlhttp.onreadystatechange = function() 
        {
            alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)

            if (xmlhttp.readyState==4) 
            {
                alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)
                if (xmlhttp.status==200)
                {
                    alert("An email with your password has been sent to the account you gave with your CO Gold submission.  It may take a few minutes to arrive.  Please also check your spam folder.");
                } // if (xmlhttp.status==200)
            } // if ((xmlhttp.readyState==4) && (xmlhttp.status==200))
            else
            {
                alert("There was a problem sending the email with your password.  Please click 'Forgot Password' again.  If you get this alert a third time, please email...");
            } // if ((xmlhttp.readyState==4) && (xmlhttp.status==200)) else
        } // xmlhttp.onreadystatechange = function() 

变为

xmlhttp.onreadystatechange = function() 
        {
            alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)

            if (xmlhttp.readyState==4) 
            {
                alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)
                if (xmlhttp.status==200)
                {
                    alert("An email with your password has been sent to the account you gave with your CO Gold submission.  It may take a few minutes to arrive.  Please also check your spam folder.");
                } // if (xmlhttp.status==200)
                else
                {
                    alert("There was a problem sending the email with your password.  Please click 'Forgot Password' again.  If you get this alert a third time, please email...");
                } // if ((xmlhttp.readyState==4) && (xmlhttp.status==200)) else
            } // if ((xmlhttp.readyState==4) && (xmlhttp.status==200))
        } // xmlhttp.onreadystatechange = function()