Javascript “不输入”;xmlhttp.onreadystatechange=function(){";
我正在关注我的旧问题帖子,突然出现了一个新问题:我的代码没有在这一行“xmlhttp.onreadystatechange=function()”中输入。我还通过标记alert进行了检查。因此我无法理解。有人能告诉我为什么没有发生这种情况吗Javascript “不输入”;xmlhttp.onreadystatechange=function(){";,javascript,Javascript,我正在关注我的旧问题帖子,突然出现了一个新问题:我的代码没有在这一行“xmlhttp.onreadystatechange=function()”中输入。我还通过标记alert进行了检查。因此我无法理解。有人能告诉我为什么没有发生这种情况吗 <html> <head> <script> function changeThis(){ xmlHttp = new XMLHttpRe
<html>
<head>
<script>
function changeThis(){
xmlHttp = new XMLHttpRequest();
var formInput = document.getElementById('theInput').value;
/* formInput will be undefined */
document.getElementById('newText').innerHTML = formInput;
/* also undefined */
// var xmlHttp = null;
xmlhttp.onreadystatechange=function() //help
{
alert("y"); // not entering help?????
if (xmlhttp.readystate==4 && http.status==200)
{
document.getElemenById('newText').innerHTML=xmlhttp.reponseText;
}
if(xmlhttp.status == 404)
{
var temp = "NO file found";
document.getElementById('newText').innerHTML=temp;
}
xmlHttp.open( "GET", "file2.php", true);
xmlHttp.send();
}
}
</script>
</head>
<body>
<p>You wrote: <span id='newText'></span> </p>
<textarea id="theInput" style="height:200px;">Write Here</textarea>
<input type='button' onclick='changeThis()' value='See what you wrote'/>
</body>
</html>
函数changeThis(){
xmlHttp=新的XMLHttpRequest();
var formInput=document.getElementById('theInput')。值;
/*formInput将是未定义的*/
document.getElementById('newText')。innerHTML=formInput;
/*也未定义*/
//var xmlHttp=null;
xmlhttp.onreadystatechange=function()//帮助
{
警报(“y”);//未输入帮助?????
if(xmlhttp.readystate==4&&http.status==200)
{
document.getElemenById('newText').innerHTML=xmlhttp.reponseText;
}
if(xmlhttp.status==404)
{
var temp=“未找到文件”;
document.getElementById('newText')。innerHTML=temp;
}
open(“GET”,“file2.php”,true);
xmlHttp.send();
}
}
你写道:
写在这里
移动此项:
xmlHttp.open( "GET", "file2.php", true);
xmlHttp.send();
在onreadystatechange
处理程序之外;在HTTP请求取得进展之前,您当前的代码不会尝试提交HTTP请求。(编辑问题以正确缩进代码的提示,使此问题显而易见。)
编辑后添加:此外,正如所指出的,您需要一致地编写
xmlHttp
。JavaScript变量名称区分大小写,因此xmlHttp
是一个完全不同的变量。我这样做了,但xmlHttp.onreadystatechange函数中没有输入任何参数。如果您能在代码中看到我的注释,您可以你可能理解我的问题,也许你能找到我做错的地方。@user3162878也请注意大写。JavaScript变量区分大小写,所以xmlhttp
与xmlhttp
不同。我在哪里应用你的建议?它是像这个xmlhttp.open(..)而不是xmlhttp.open(“GET”,“file2.php”,true);和xmlHttp.send()????