【问题标题】:xmlhttp.readyState returns 2 even though xmlhttp.status equals 200xmlhttp.readyState 返回 2 即使 xmlhttp.status 等于 200
【发布时间】:2016-07-02 15:50:15
【问题描述】:

我正在通过 ajax 发送电子邮件以联系 php。 php 脚本成功发送电子邮件,但即使 xmlhttp.status 为 200,ajax xmlhttp.readyState 仍在继续返回 2。

params = "name=" + name + "&email=" + email + "&message=" + message + "&telephone=" + telephone;

xmlhttp.open("POST", "contact.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){

        if(xmlhttp.responseText == "fill_form"){
            note.innerHTML = "Please fill the required fields properly";
            return;
        }

        if(xmlhttp.responseText == "Sent"){
            serverMessage.innerHTML = "Thanks. If it is a request or complaint we well get back to you soon";
        }
    }
    else{
        serverMessage.innerHTML = "Some internal error occured while sending the email. Please try again later";
        $('#myModal').modal('show')
    }

    submitBtn.innerHTML = "SEND MESSAGE";
    submitBtn.disabled = false;
}

联系方式.php

<?php

$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$telephone=$_POST['telephone'];

$mail_to_send_to = "abc@gmail.com";
$feed_back_mail = "name@myDomainName.com";

if (empty($name) || empty($email)|| empty($message))
{
    echo "fill_form";
}
else{

    $from="From:$feed_back_mail"."\r\n"."Reply-To:$email"."\r\n" ;
    $subject="Users feed back Contact";

    if(empty($telephone)){
        $telephone = "No telephone sent my user";    
    }

    $message = "Telephone: $telephone\r\nSender's Email : $email \r\n \r\n$message \r\n";

$isSent = mail($mail_to_send_to, $subject, $message, $from);

    if($isSent){
        echo $isSent;
    }
    else{
        echo "not_sent";
    }
}

?>

做错了什么?

【问题讨论】:

    标签: javascript php


    【解决方案1】:

    请记住,您的 onreadystatechange 回调将在就绪状态更改时被多次调用。您当前的代码响应期望它完成的 first 回调。但是,之前收到readyState2(“收到的标头”)的电话是很正常的。

    所以等到你收到readyState 4:

    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4){
            // Done, what happened?
            if(xmlhttp.status == 200){
                // All good
            }
            else{
                // Something went wrong
            }
        }
    };
    

    【讨论】:

      【解决方案2】:

      xmlhttp.readyState 持有 XMLHttpRequest 的状态。

      可以是0到4:

      0: request not initialized 
      1: server connection established
      2: request received 
      3: processing request 
      4: request finished and response is ready
      

      xmlhttp.status 表示你刚刚提出的请求的状态码。

      更多来自developer.mozilla.org

      XMLHttpRequest.readyState 属性返回 XMLHttpRequest 客户端所处的状态。

      0   UNSENT              Client has been created. open() not called yet.
      1   OPENED              open() has been called.
      2   HEADERS_RECEIVED    send() has been called, and headers and status are available.
      3   LOADING             Downloading; responseText holds partial data.
      4   DONE                The operation is complete.
      

      2 表示 send() 已被调用并已收到响应头。

      4 表示 提取操作完成。这可能意味着数据传输已成功完成或失败。

      所以你应该一直等待 readyState 4

      https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

      【讨论】:

        猜你喜欢
        • 2011-09-09
        • 2016-11-14
        • 2015-03-16
        • 2017-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-09
        相关资源
        最近更新 更多