问下xhr.status == 200和xhr.readyState == 4 的区别是什么,为什么两者要同时判断啊

解决方案 »

  1.   

    xhr.readyState == 4  是表示后台处理完成了。
    xhr.status == 200 是表示处理的结果是OK的。
      

  2.   

    xhr.readyState == 4
    表示接受结果完毕了。
      

  3.   

    麻烦大侠看看为什么不出结果呢
    代码如下
    <!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>
      

  4.   

    当不加if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) 这句可以
      

  5.   

    你是本地file测试的吧。。这样status为0,返回200或者5000,404状态状态需要搭建服务器访问才会返回200或者500,404状态成功判断200或者0状态就行了,转向不用判断的,会自动返回最后跳转的网址的状态            if(xhr.status == 200  || xhr.status == 0) {
                     
                    document.getElementById('divTip').innerHTML = xhr.responseText;
                     
                }
      

  6.   

    http 状态码 200到300是指  服务端正常返回
    304  是告诉客户端取缓存数据
    所以才这么写 xhr.status >= 200 && xhr.status < 300 || xhr.status == 304
    http  状态码
    但是你说 你不写这个 才能接受到数据
    那就是说 服务端出状况啦 ...........
      

  7.   


    HTTP请求模型和头信息标准说法是重定向,如你访问a.asp,a.asp有response.redirect "b.asp",那么就会重定向到b.asp页面,如果b.asp不存在【404】,有错误【500】,正确【200】,那么ajax最后得到的状态为列出来的3中状况,所以判断200就行了本地的话成功执行返回0状态