xmlhttp.readyState返回2,即使xmlhttp.status等于200

时间:2016-07-02 07:45:21

标签: javascript php

我正在通过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;
}

Contact.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";
    }
}

&GT;

出了什么问题?

2 个答案:

答案 0 :(得分:1)

请记住,当就绪状态发生变化时,您的onreadystatechange回调将被称为多次。您当前的代码会响应第一个回调,期望它完成。但是,使用readyState 2(&#34;收到标题&#34;)之前接听电话是很正常的。

所以等到你得到readyState 4:

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

答案 1 :(得分:-1)

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