gpt4 book ai didi

javascript - onreadystatechange 函数未被调用

转载 作者:太空宇宙 更新时间:2023-11-04 14:49:14 27 4
gpt4 key购买 nike

由于某些原因,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>

最佳答案

你在 onreadystatechange 函数中有一个错误:

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

xmlHttpreadyState 应该是 xmlHttp.readyState

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

关于javascript - onreadystatechange 函数未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/403165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com