利用Asp.net Ajax异步获取xml文档内容

yibin 2015-02-09 建站源码 518

Ajax原意本是让人利用异步获取XML内容,以实现无刷新的网页效果
asp.net ajax已将底层内容封装
实现起来远比自写xmlhttp来的方便,至少浏览器兼容方面要强得很多
下面以一个www.asp.net的示例说明一下利用microsoft ajax library来简单异步获取xml文档的方法
// 返回XML的Web 请求
function OnSucceededXml(executor, eventArgs)
...{
  if (executor.get_responseAvailable())
  ...{
    if (document.all)
      resultElementId.innerText += executor.get_xml().xml;
    else
      // Firefox
      resultElementId.textContent += "First node[首节点]: " +
        executor.get_xml().documentElement.nodeName;
  
  }
  else
  ...{
    if (executor.get_timedOut())
      alert("超时");
    else
      if (executor.get_aborted())
        alert("失败");
  }
}
function GetXml()
...{
  //创建WebRequest对象
  wRequest = new Sys.Net.WebRequest();
  
  //设置请求文件
  wRequest.set_url("index.xml");
  // Set the request handler.
  wRequest.add_completed(OnSucceededXml);
  // Clear the results area.
  if (document.all)
    resultElementId.innerText = "";
  else
    // Firefox
    resultElementId.textContent = "";
  // Invoke the Web request.
  wRequest.invoke();
}
<button id="Button3"
            onclick="GetXml()">Xml</button>
<div id="ResultId" style="background-color:Aqua;"></div>

扫码添加微信

13013082126 扫描微信 建站咨询 优化咨询