如何将xmlhttprequest存储到变量中

时间:2011-01-24 11:26:21

标签: javascript html xmlhttprequest

我有一个简单的问题,你聪明的家伙也许有答案。 为什么这样做

<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc("ajax_info.txt",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xlmhttp.responseText;
    }
  });
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction()">Change Content</button>

</body>
</html>

但如果我更换

document.getElementById("myDiv").innerHTML=xlmhttp.responseText;

白衣

var txt=xlmhttp.responseText;
document.getElementById("myDiv").innerHTML=txt;

它不再起作用,txt是

  

未定义

。如何将xlmhttp.responseText存储到字符串中,或​​存储到我可以执行搜索的变量中?请举例说明我如何做到这一点。在此先感谢=)

1 个答案:

答案 0 :(得分:2)

可能是因为你有拼写错误的变量“xmlhttp” - 你的代码中有“xlmhttp”。

相关问题