xhr.status == 200和xhr.readyState == 4 的区别是什么 问下xhr.status == 200和xhr.readyState == 4 的区别是什么,为什么两者要同时判断啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 xhr.readyState == 4 是表示后台处理完成了。xhr.status == 200 是表示处理的结果是OK的。 xhr.readyState == 4表示接受结果完毕了。 麻烦大侠看看为什么不出结果呢代码如下<!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> 当不加if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) 这句可以 你是本地file测试的吧。。这样status为0,返回200或者5000,404状态状态需要搭建服务器访问才会返回200或者500,404状态成功判断200或者0状态就行了,转向不用判断的,会自动返回最后跳转的网址的状态 if(xhr.status == 200 || xhr.status == 0) { document.getElementById('divTip').innerHTML = xhr.responseText; } http 状态码 200到300是指 服务端正常返回304 是告诉客户端取缓存数据所以才这么写 xhr.status >= 200 && xhr.status < 300 || xhr.status == 304http 状态码但是你说 你不写这个 才能接受到数据那就是说 服务端出状况啦 ........... HTTP请求模型和头信息标准说法是重定向,如你访问a.asp,a.asp有response.redirect "b.asp",那么就会重定向到b.asp页面,如果b.asp不存在【404】,有错误【500】,正确【200】,那么ajax最后得到的状态为列出来的3中状况,所以判断200就行了本地的话成功执行返回0状态 一个简单方法求解释 JSP开发求解:利用js实现子页面向父页面赋值 javascipt访问问题 ExtJS3.4无法的异步树无法加载数据 <option selected>为什么显示不出来?! 关于窗口打开的两个问题. JS实现登录刷新效果,如何实现? 怎样用链接触发按钮提交网页? 没分了,大家能不能帮忙看看呢? 怎样才能在页面上直接显示日期和时间。 请教一个关于命名空间理解的问题,是不是就给全局变量和函数变成了某对象的属性和方法了? 将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状态