function postData(postString, postUrl) {    var xmlHttp = createXml();
    xmlHttp.open("POST", postUrl + "&n=" + Math.random(), true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(postString);
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
                return xmlHttp.responseText;        }
    }
    
    if (xmlHttp.readyState == 1) { //document.getElementById('out').innerText = '请求已提出(调用 send() 之前)';
        return  "请求已提出";
    }
    if (xmlHttp.readyState == 2) { //document.getElementById('out').innerText = '请求已发送(这里通常可以从响应得到内容头部)';
        return  "请求已发送";
    }
    if (xmlHttp.readyState == 3) { //document.getElementById('out').innerText = '请求处理中(响应中通常有部分数据可用,但是服务器还没有完成响应)';
        return "请求处理中";
    } }
一段ajax function 当使用这个方法的时候 xmlHttp.readyState 不同时刻 不同的值  但是return时候function就终止了 所以引用这个function显示的内容都是 “请求已提出”如何修改这个return 可以不让终止
我知道可以用 把return 换成document.xxxx.innerHTML=''就可以 但是相统一一个方法 所以有什么好的方法么

解决方案 »

  1.   


    function postData(postString, postUrl) {    var xmlHttp = createXml();
        xmlHttp.open("POST", postUrl + "&n=" + Math.random(), true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.send(postString);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                    return xmlHttp.responseText;        }
        }
        var result;
        if (xmlHttp.readyState == 1) { //document.getElementById('out').innerText = '请求已提出(调用 send() 之前)';
            result="请求已提出";
        }
        if (xmlHttp.readyState == 2) { //document.getElementById('out').innerText = '请求已发送(这里通常可以从响应得到内容头部)';
            result="请求已发送";
        }
        if (xmlHttp.readyState == 3) { //document.getElementById('out').innerText = '请求处理中(响应中通常有部分数据可用,但是服务器还没有完成响应)';
            result= "请求处理中";
        }
       //你既然不想让if内的retun结束方法,那就在此进行后续的操作
       //最后在return
        retrun result;
     }