xmlhttp.readyState返回2,即使xmlhttp.状态等于200
xmlhttp.readyState returns 2 even though xmlhttp.status equals 200
我通过ajax发送电子邮件联系php。Php脚本发送邮件成功,但ajax xmlhttp。readyState一直返回2,即使xmlhttp。状态为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 = "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";
}
}
?>
我做错了什么?
请记住,随着就绪状态的变化,您的onreadystatechange
回调将被多次调用。您当前的代码响应第一个回调,期望它完成。但是对于您来说,在之前得到一个呼叫是很正常的,其中包含readyState
2 ("headers received")。
所以只要等到你得到readyState
4:
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
// Done, what happened?
if(xmlhttp.status == 200){
// All good
}
else{
// Something went wrong
}
}
};
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.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState2表示已经调用send()并且已经收到响应头。
4表示读取操作完成。这可能意味着数据传输已经成功完成,也可能失败。
所以你应该总是等待readyState 4。
相关文章:
- 事件和状态
- 获取选择框的状态
- 相位器状态未捕获参考错误
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何使用密码检测网络中的状态连接
- Ember.js-接口状态应该存储在哪里
- xmlhttp.responseText不显示文本
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- 在Angular 2中布线期间保持零部件处于活动状态
- 在mvc应用程序中,在回发时保留最初隐藏的文本框的隐藏或可见状态
- XMLHttpRequest未返回值-状态202
- 使用javascript反复检查用户在facebook上的登录状态
- 如何使bxslider仅在移动视图中处于活动状态
- 在 Forefox 中获取 XMLHTTP 状态 401,同时从 JavaScript 对经过 Windows 身份验证
- xmlhttp请求状态302
- xmlhttp请求状态总是返回0
- xmlhttp请求具有奇怪的行为,并且状态为0
- 在IE11中设置xmlHttp响应类型为msxml-document会抛出无效状态错误
- 如何抑制javascript错误时,xmlhttp.状态!= 200
- xmlhttp.readyState返回2,即使xmlhttp.状态等于200