问下xhr.status == 200和xhr.readyState == 4 的区别是什么,为什么两者要同时判断啊
解决方案 »
- 火狐不支持这个JS吗?mydiv.style.top=150;
- 求各位大虾们帮帮忙啊,怎么让这个切换效果实现自动切换。
- js创建img并添加点击事件
- 使用window.setTimeout()+ajax 定时无刷新 实现Web在线客服中的消息查询,不知是否可行??请教大家
- 如何用JS打开新窗口,并关闭原来窗口
- JS 打印 factory.printing 有没有打印背景的 属性?怎么用?
- 在浏览器中,用JS怎么判断一组图片是否下载完成?
- 如何得到radio得值?
- 如何关闭一个弹出窗口后自动弹出另一个?急!多谢
- 请qisanyou(www.qisanyou.com)快来拿分。
- 请教一个关于命名空间理解的问题,是不是就给全局变量和函数变成了某对象的属性和方法了?
- 将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状态