开发者

how to store a xmlhttprequest into a variable

I have a quick question that you brainy guys perhaps have the answer to. Why does this work

<html>
<head>
<script type="text/javascript">
var xmlhttp;
func开发者_C百科tion 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>

but if I replace

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

whit this

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

it don't work anymore, txt is

undefined

. How can I store the xlmhttp.responseText into a string, or into a variable that I can perform a search on? Please give an example on how I can do it. Thanks in advance =)


Might be because you have misspelled variable "xmlhttp" - you have "xlmhttp" in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜