AJAX一些常用操作

来源:互联网 发布:cad可以画网络拓扑图吗 编辑:程序博客网 时间:2024/05/17 00:57

加载XML:

function loadXMLDoc(url){var xmlhttp;var txt,xx,x,i;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)    {    txt="<table border='1'><tr><th>Title</th><th>Artist</th></tr>";    x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");    for (i=0;i<x.length;i++)      {      txt=txt + "<tr>";      xx=x[i].getElementsByTagName("TITLE");        {        try          {          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";          }        catch (er)          {          txt=txt + "<td>&nbsp;</td>";          }        }    xx=x[i].getElementsByTagName("ARTIST");      {        try          {          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";          }        catch (er)          {          txt=txt + "<td>&nbsp;</td>";          }        }      txt=txt + "</tr>";      }    txt=txt + "</table>";    document.getElementById('txtCDInfo').innerHTML=txt;    }  }xmlhttp.open("GET",url,true);xmlhttp.send();

获取头信息:

<!DOCTYPE html><html><head><script>function loadXMLDoc(url){var xmlhttp;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('p1').innerHTML=xmlhttp.getAllResponseHeaders();    }  }xmlhttp.open("GET",url,true);xmlhttp.send();}</script></head><body><p id="p1"></p><button onclick="loadXMLDoc('/try/ajax/ajax_info.txt')">Get header information</button></body></html>
0 0
原创粉丝点击