当前位置: 首页 > 图文教程 > 网络编程 > AJAX技术 > AJAX教程(14):通过XMLHTTP加载XML文件

AJAX技术
用AJAX技术聚合RSS
Ajax 网址备忘
用javascript实现页面无刷新更新数据
ajax应用
AJAX入门之XMLHttpRequest慨述
AJAX入门之深入理解JavaScript中的函数
XMLHttpRequest of ajax
ajax 检测用户名是否被占用
开始研究Ajax. 第一天
ajax 不错的应用
Ajax技术(WEB无刷新提交数据)-
AJAX应用之注册用户即时检测
AJAX初体验之上手篇
ASP小偷程序如何利用XMLHTTP实现表单的提交
AJAX打造博客无刷新搜索
Flash & Ajax 操作 XML 实例:无刷新分页
Ajax 汇总以及初步评价
基于 Ajax 的无限级菜单
AJAX请求类
发布三个ajax相关的函数,包括无刷新提交表单等

AJAX技术 中的 AJAX教程(14):通过XMLHTTP加载XML文件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 42 ::
收藏到网摘: n/a

通过XMLHTTP加载XML文件:

<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Opera, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('A1').innerHTML=xmlhttp.status;
    document.getElementById('A2').innerHTML=xmlhttp.statusText;
    document.getElementById('A3').innerHTML=xmlhttp.responseText;
    }
  else
    {
    alert("Problem retrieving XML data:" + xmlhttp.statusText);
    }
  }
}
</script>
</head>

<body>
<h2>Using the HttpRequest Object</h2>

<p><b>Status:</b>
<span id="A1"></span>
</p>

<p><b>Status text:</b>
<span id="A2"></span>
</p>

<p><b>Response:</b>
<br /><span id="A3"></span>
</p>

<button onclick="loadXMLDoc('/example/xmle/note.xml')">Get XML</button>

</body>
</html>