程序员

用Ajax读取XML格式的数据

作者:admin 2021-08-17 我要评论

复制代码 代码如下: !DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" html head titleAjaxHelloWorld...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<title>Ajax Hello World</title> 
<script type="text/javascript"> 
var xmlHttp; 

function createXMLHttpRequest(){ 
    if(window.ActiveXObject){ 
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    else if(window.XMLHttpRequest){ 
        xmlHttp = new XMLHttpRequest(); 
    } 


function startRequest(){ 
    createXMLHttpRequest(); 
    try{ 
        xmlHttp.onreadystatechange = handleStateChange; 
        xmlHttp.open("GET", "data.xml", true); 
        xmlHttp.send(null);     
    }catch(exception){ 
        alert("您要访问的资源不存在!"); 
    } 


function handleStateChange(){     
    if(xmlHttp.readyState == 4){         
        if (xmlHttp.status == 200 || xmlHttp.status == 0){ 
            // 取得XML的DOM对象 
            var xmlDOM = xmlHttp.responseXML; 
            // 取得XML文档的根 
            var root = xmlDOM.documentElement;             
            try 
            { 
                // 取得<info>结果 
                var info = root.getElementsByTagName('info'); 
                // 显示返回结果 
                alert("responseXML's value: " + info[0].firstChild.data); 
            }catch(exception) 
            { 

            } 
        } 
    } 

</script> 
</head> 
<body> 
    <div> 
        <input type="button" value="return ajax responseXML's value" 
                onclick="startRequest();" /> 
    </div> 
</body> 
</html>

server.xml
复制代码 代码如下:

<?xml version="1.0" encoding="GB2312" ?> 
<root>
    <info>hello world!</info>
</root>


原文链接:https://m.jb51.net/article/4192.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 十月更新修复了Windows 10的Ping of De

    十月更新修复了Windows 10的Ping of De

  • Windows10 UAC弹窗太烦但又不能关?教

    Windows10 UAC弹窗太烦但又不能关?教

  • 老大手把手教我玩 Git 变基!

    老大手把手教我玩 Git 变基!

  • 在Linux终端中展示幻灯片

    在Linux终端中展示幻灯片