使用AJAX与后台交互局部数据

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function loadXMLDoc()
 5 {
 6   var xmlhttp;
 7   if (window.XMLHttpRequest)
 8   {// code for IE7+, Firefox, Chrome, Opera, Safari
 9     xmlhttp=new XMLHttpRequest();
10   }
11   else
12   {// code for IE6, IE5
13     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
14   }
15   xmlhttp.onreadystatechange=function()
16    {
17     if (xmlhttp.readyState==4 && xmlhttp.status==200)
18      {
19         document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
20      }
21     }
22   xmlhttp.open("POST","/ajax/demo_post.asp",true);
23   xmlhttp.send();
<!-- post表单

        xmlhttp.open("POST","/ajax/demo_post2.asp",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("fname=Bill&lname=Gates");

      当您使用 async=false 时,请不要编写 onreadystatechange 函数 - 把代码放到 send() 语句后面即可:
      xmlhttp.open("GET","test1.txt",false);
      xmlhttp.send();
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

        如果来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,请使用 responseXML 属性:

        xmlDoc=xmlhttp.responseXML;
        txt="";
        x=xmlDoc.getElementsByTagName("title");
        for (i=0;i<x.length;i++)
        {
           txt=txt + x[i].childNodes[0].nodeValue + "<br />";
        }
        document.getElementById("myDiv").innerHTML=txt;


-->
24 } 25 </script> 26 </head> 27 <body> 28 29 <h2>AJAX</h2> 30 <button type="button" onclick="loadXMLDoc()">请求数据</button> 31 <div id="myDiv"></div> 32 33 </body> 34 </html>

<!--

<html>
<head>
<script type="text/javascript">
function showHint(str)
{
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
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=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/ajax/gethint.asp?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>


<h3>请在下面的输入框中键入字母(A - Z):</h3>
<form action="">
姓氏:<input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>建议:<span id="txtHint"></span></p>

</body>
</html>

 -->

 

posted on 2013-02-19 21:57  云眸  阅读(304)  评论(0编辑  收藏  举报

导航