首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用onreadystatechange函数

未调用onreadystatechange函数
EN

Stack Overflow用户
提问于 2008-12-31 15:08:15
回答 2查看 10.7K关注 0票数 2

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

代码语言:javascript
复制
    <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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-12-31 15:40:18

onreadystatechange函数中有一个错误:

代码语言:javascript
复制
alert("entered call back function. readstate value is: "+xmlHttpreadyState+". Response Text is: "+xmlHttp.responseText);

xmlHttpreadyState应为xmlHttp.readyState

在我修复了这个问题之后,它在FF3中对我起作用了

票数 6
EN

Stack Overflow用户

发布于 2008-12-31 15:37:25

如果我错了,请纠正我,但是对于POST,你不需要像这样做一个内容长度的setRequestHeader吗;

代码语言:javascript
复制
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader('Content-length',(params?params.length:0));
xmlHttp.send(params);

这可能会纠正您的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/403165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档