【问题标题】:onreadystatechange function not being called未调用 onreadystatechange 函数
【发布时间】:2023-04-04 22:17:01
【问题描述】:

由于某种原因,onreadystatechange 回调函数没有在异步模式下被调用。我在同步模式下测试了帖子并确认帖子本身工作正常(注释掉我用来在同步模式下检查帖子的测试代码)。该问题出现在 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>

【问题讨论】:

    标签: javascript html ajax


    【解决方案1】:

    你的onreadystatechange函数有错误:

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

    xmlHttpreadyState 应该是xmlHttp.readyState

    修复后,它在 FF3 中对我有用

    【讨论】:

    • 谢谢。有效。尽管多次查看代码,但我不敢相信我错过了它。
    【解决方案2】:

    如果我错了,请纠正我,但是对于 POST,你不需要像这样为 Content-Length 做一个 setRequestHeader;

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

    这可能会解决您的问题。

    【讨论】:

    • 它在不添加该行的情况下工作。我会进一步研究你的观点。我真的很感谢你的帮助。 RoBorg 的回答解决了我的问题
    猜你喜欢
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 2012-12-27
    • 2011-06-24
    相关资源
    最近更新 更多