Javascript 未调用onreadystatechange函数

Javascript 未调用onreadystatechange函数,javascript,html,ajax,Javascript,Html,Ajax,由于某些原因,onreadystatechange回调函数未在异步模式下调用。我在同步模式下测试了post,并确认post本身工作正常(注释掉了我用来在同步模式下检查post的测试代码)。这个问题在safari和firefox的最新版本中都会出现。有人能告诉我我做错了什么吗?多谢各位 <html> <head> <script> function recordScore(str) { if (str.lengt

由于某些原因,onreadystatechange回调函数未在异步模式下调用。我在同步模式下测试了post,并确认post本身工作正常(注释掉了我用来在同步模式下检查post的测试代码)。这个问题在safari和firefox的最新版本中都会出现。有人能告诉我我做错了什么吗?多谢各位

    <html>
    <head>
    <script>
    function recordScore(str)
    {

    if (str.length==0)
    { 

        return;
    }

    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 

    var url="http://hellworld3.appspot.com/findcountry";
    var params = "screenname="+document.getElementById("screenname1").value+"&score="+document.getElementById("score1").value;
    alert("params: "+params);
    xmlHttp.open("POST",url,true);

    xmlHttp.onreadystatechange = function() 
    {
        alert("entered call back function. readstate value is: "+xmlHttpreadyState+". Response Text is: "+xmlHttp.responseText);

    if (xmlHttp.readyState==4)
    { 
        document.getElementById("message").innerHTML=xmlHttp.responseText;
    }
    }
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    xmlHttp.send(params);

    //Testing code for synchronous mode
    //alert("Http get status is: "+xmlHttp.status);
    //alert("Http ready state value is: "+xmlHttp.readyState);
    //alert("Http get response text is: "+xmlHttp.responseText);
    //document.getElementById("message").innerHTML=xmlHttp.responseText;
    }


    function GetXmlHttpObject()
    {
        var xmlHttp=null;
        try
        {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
            // Internet Explorer
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }

    </script>


    </head>


    <body>

    <form name="testform">
        Screename: 
        <input type="text" id="screenname1" name="screenname">
        <br/>
        Score:
        <input type="text" id="score1" name="score" onchange="recordScore(this.value)"> 
        <br/>
        <p id="message">test</p>

        <input type="submit" value="Submit">
    </form>

    </body>


    </html>

功能记录分数(str)
{
如果(str.length==0)
{ 
返回;
}
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null)
{
警告(“您的浏览器不支持AJAX!”);
返回;
} 
变量url=”http://hellworld3.appspot.com/findcountry";
var params=“screenname=“+document.getElementById(“screenname1”).value+”&score=“+document.getElementById(“score1”).value;
警报(“参数:+params”);
open(“POST”,url,true);
xmlHttp.onreadystatechange=函数()
{
警报(“输入的回调函数。readstate值为:“+xmlHttpreadyState+”。响应文本为:“+xmlHttp.responseText”);
if(xmlHttp.readyState==4)
{ 
document.getElementById(“message”).innerHTML=xmlHttp.responseText;
}
}
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”)
xmlHttp.send(params);
//同步模式测试代码
//警报(“Http get状态为:”+xmlHttp.status);
//警报(“Http就绪状态值为:”+xmlHttp.readyState);
//警报(“Http获取响应文本为:”+xmlHttp.responseText);
//document.getElementById(“message”).innerHTML=xmlHttp.responseText;
}
函数GetXmlHttpObject()
{
var xmlHttp=null;
尝试
{
//Firefox、Opera 8.0+、Safari
xmlHttp=新的XMLHttpRequest();
}
捕获(e)
{
//Internet Explorer
尝试
{
xmlHttp=新的ActiveXObject(“Msxml2.xmlHttp”);
}
捕获(e)
{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}
}
返回xmlHttp;
}
屏幕名称:

分数:
测试


如果我错了,请纠正我,但对于POST,您不需要像这样为内容长度执行setRequestHeader

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader('Content-length',(params?params.length:0));
xmlHttp.send(params);

这可能会纠正您的问题。

onreadystatechange函数中有一个错误:

alert("entered call back function. readstate value is: "+xmlHttpreadyState+". Response Text is: "+xmlHttp.responseText);
xmlHttpreadyState
应该是
xmlHttp.readyState


在我修复了它之后,它在FF3中为我工作了

谢谢。成功了。我不敢相信,尽管我对代码进行了多次检查,但还是漏掉了它。它在没有添加那行代码的情况下工作了。我将进一步研究你的观点。我真的很感谢你的帮助。罗博格的回答解决了我的问题