问下xhr.status == 200和xhr.readyState == 4 的区别是什么,为什么两者要同时判断啊
解决方案 »
- javascript怎样以绝对地址的方式取得frame
- javascript怎么取得<textarea>标签里的值
- 关于JS和ActiveX之间事件的问题
- 这个超链接有什么问题
- 关于表单文本框同一名字问题,急需各位高手解答
- 如何用JS或者VBS读出收藏夹的内容?
- 火狐网站这个页面根据鼠标滚轮变化
- 服务器端Xml文件正常,下载到客户端后出错:不支持从当前编码到指定编码的切换
- 急救!!!日期select...
- bootstrap-datetimepicker切换时间选择面板时,会清空表单输入问题
- 请教一个关于命名空间理解的问题,是不是就给全局变量和函数变成了某对象的属性和方法了?
- 将java对象转成json串的异常
xhr.status == 200 是表示处理的结果是OK的。
表示接受结果完毕了。
代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="scripts/jquery-1.4.4.min.js"></script>
<style type="text/css">
body { margin:0; padding:0; }
.divFrame { border:1px solid #000; width:150px; }
#buttons { border:1px solid #000; }
.divContent { padding:5px; }
.divTitle { padding:3px; background:#CCC; }
</style>
</head><body>
<div class="divFrame">
<div class="divTitle">
<input type="button" id="buttons" value="提交" />
</div>
<div class="divContent">
<div id="divTip"></div>
</div>
</div>
<script type="text/javascript">
function createXHR() {
var xhr = null;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
return xhr;
} else if(window.ActiveXObject) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
return xhr;
} else {
alert('初始化XMLHTTP错误');
}
}function getSendData() {
//document.getElementById('divTip').innerHTML = '<img src="Loading.gif" title="正在加载中......" />';
var xhr = createXHR();
url = 'untitled.html?data=' + Date();
xhr.open('GET', url, false);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
document.getElementById('divTip').innerHTML = xhr.responseText;
}
}
};
xhr.send(null);
}document.getElementById('buttons').onclick = getSendData;
/*$('#buttons').click(function() {
$('#divTip').load('untitled.html');
});*/
</script>
</body>
</html>
document.getElementById('divTip').innerHTML = xhr.responseText;
}
304 是告诉客户端取缓存数据
所以才这么写 xhr.status >= 200 && xhr.status < 300 || xhr.status == 304
http 状态码
但是你说 你不写这个 才能接受到数据
那就是说 服务端出状况啦 ...........
HTTP请求模型和头信息标准说法是重定向,如你访问a.asp,a.asp有response.redirect "b.asp",那么就会重定向到b.asp页面,如果b.asp不存在【404】,有错误【500】,正确【200】,那么ajax最后得到的状态为列出来的3中状况,所以判断200就行了本地的话成功执行返回0状态