2016-07-02 100 views
1

我正在通過ajax發送一封電子郵件以聯繫php。 PHP腳本成功發送電子郵件,但阿賈克斯xmlhttp.readyState是保持returing 2即使xmlhttp.status是200xmlhttp.readyState返回2,即使xmlhttp.status等於200

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 = "[email protected]"; 
$feed_back_mail = "[email protected]"; 

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

?>

什麼我做錯誤?

回答

1

記住,你onreadystatechange回調將被稱爲多次爲就緒狀態的變化。您當前的代碼對第一個回調作出了響應,預計它會完成。但在與readyState 2(「收到標題」)之前撥打電話是很正常的。

所以纔等到你readyState 4:

xmlhttp.onreadystatechange = function(){ 
    if(xmlhttp.readyState == 4){ 
     // Done, what happened? 
     if(xmlhttp.status == 200){ 
      // All good 
     } 
     else{ 
      // Something went wrong 
     } 
    } 
}; 
-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. 

表示發送()被調用和響應標頭具有已收到。

指示獲取操作完成。這可能意味着數據傳輸已成功完成或失敗。

因此,你應該一直等待readyState 。

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

+0

那麼,爲什麼不xmlhttp.readyState是4? – ash

+0

請檢查我的更新答案! –

相關問題