function doRequestUsingPost(url,postdata)
{
    createxmlHttpRequest();
    var queryString = createQueryString(postdata);
     
    xmlHttp.open("POST",url,false);    //false表示同步模式,true异步模式
    //alert(queryString);
    //xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.onreadystatechange=handleStateChange();
//这里为啥加括号了?加括号的话,是在解析到这条语句时,就执行了,但是此时还没有发出请求,返回的值,肯定不符合readyState=4和status=200的情况啊。你上面注释掉的那句,用的话,也会出错吗?
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(queryString);
    alert('after nong!');
}
 
function handleStateChange()
{
    if(xmlHttp.readyState==4 && xmlHttp.status == 200)
    {        
        alert(xmlHttp.responseText);
    }
//我觉得这里你可以改一下,看看到底执行到哪里了,改成下面这样。
if(xmlHttp.readyState==4){
console.log("readyState = 4!");
console.log("status = "+xmlHttp.status);
//看下status值,是不是等于200的。
if(xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}你这里xmlHttp是当做全局变量用的啊。

解决方案 »

  1.   

        function doRequestUsingPost(url, postdata) {
            createxmlHttpRequest();
            var queryString = createQueryString(postdata);        xmlHttp.open("POST", url, false);    //false表示同步模式,true异步模式
            //alert(queryString);
            //xmlHttp.onreadystatechange = handleStateChange;
            xmlHttp.onreadystatechange = function () { handleStateChange(xmlHttp); }
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHttp.send(queryString);
            alert('after nong!');
        }    function handleStateChange(xmlHttp) {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                alert(xmlHttp.responseText);
            }