Ajax解析数据

前端之家收集整理的这篇文章主要介绍了Ajax解析数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

解析json数据

{"person":{
    "name":"QIANG","website":"zqwang121@sina.com","email":"zqwang121@sina.com"
}
}

[JavaScript]代码

<script type="text/javascript">
    window.onload=function(){
        var aNodes=document.getElementsByTagName("a");
        for(var i=0;i<aNodes.length;i++){
            aNodes[i].onclick=function(){
            var request=new XMLHttpRequest();
            var method="GET";
            var url=this.href;
            request.open(method,url);
            request.send(null);
            request.onreadystatechange=function(){
                if(request.readyState==4){
                    if(request.status==200){
                        //1.结果为json格式,所以需要使用responsexml来获取
                        var result=request.responseText;
                        var object=eval("("+result+")");
                        //2.结果不能直接使用,必须先创建对应的节点,再把节点加入到details里面
                        //目标格式为
                        /*
                        <h2><a href="zqwang121@sina.com">zqwang121</a></h2>
                        <a href="http://zqwang.cc">http://http://zqwang.cc</a>
                        */
                        var name=object.person.name;
                        var website=object.person.website;
                        var email=object.person.email;
                        //alert(name);
                        //alert(website);
                        //alert(email);
                        var aNode=document.createElement("a");      
                        aNode.appendChild(document.createTextNode(name));               
                        aNode.href="mailto:"+email;
                         
                        var h2Node=document.createElement("h2");
                        h2Node.appendChild(aNode);
                         
                        var aNode2=document.createElement("a");
                        aNode2.appendChild(document.createTextNode(website));
                        aNode2.href=website;
                         
                        var detailsNode=document.getElementById("details");
                        detailsNode.innerHTML="";
                        detailsNode.appendChild(h2Node);
                        detailsNode.appendChild(aNode2);
                         
                    }
                }
            }
                return false;
            }
        }
    }
             
</script>

[HTML]代码

<body>
  <h1>People</h1>
  <ul>
      <li><a href="1.html">1</a></li>
      <li><a href="2.html">2</a></li>
      <li><a href="3.html">3</a></li>
  </ul>
  <div id="zqwang121"></div>
</body>

解析xml数据

[XML]代码

<?xml version="1.0" encoding="UTF-8"?>
<details>
    <name>QIANG</name>
    <website>zqwang121@sina.com</website>
    <email>zqwang121@sina.com</email>
</details>

[JavaScript]代码

<script type="text/javascript">
window.onload=function(){
    var aNodes=document.getElementsByTagName("a");
    for(var i=0;i<aNodes.length;i++){
        aNodes[i].onclick=function(){
    var request=new XMLHttpRequest();
    var method="GET";
    var url=this.href;
    request.open(method,url);
    request.send(null);
    request.onreadystatechange=function(){
        if(request.readyState==4){
            if(request.status==200){
                //1.结果为xml格式,所以需要使用responsexml来获取
                var result=request.responseXML;
                //2.结果不能直接使用,必须先创建对应的节点,再把节点加入到zqwang121里面
                //目标格式为
                /*
                <h2><a href="zqwang121@sina.com">zqwang121</a></h2>
                <a href="http://zqwang.cc">http://zqwang.cc</a>
                */
                var name=result.getElementsByTagName("name")[0].firstChild.nodeValue;
                var website=result.getElementsByTagName("website")[0].firstChild.nodeValue;
                var email=result.getElementsByTagName("email")[0].firstChild.nodeValue;
                //alert(name);
                //alert(website);
                //alert(email);
                var aNode=document.createElement("a");      
                aNode.appendChild(document.createTextNode(name));               
                aNode.href="mailto:"+email;
                 
                var h2Node=document.createElement("h2");
                h2Node.appendChild(aNode);
                 
                var aNode2=document.createElement("a");
                aNode2.appendChild(document.createTextNode(website));
                aNode2.href=website;
                 
                var zqwang121Node=document.getElementById("zqwang121");
                zqwang121Node.innerHTML="";
                zqwang121Node.appendChild(h2Node);
                zqwang121Node.appendChild(aNode2);
                 
            }
        }
    }
            return false;
        }
    }
}
         
</script>

[HTML]代码

<body>
  <h1>People</h1>
  <ul>
      <li><a href="1.html">1</a></li>
      <li><a href="2.html">2</a></li>
      <li><a href="3.html">3</a></li>
  </ul>
  <div id="zqwang121"></div>
</body>

解析html数据

[JavaScript]代码

<script type="text/javascript">
    window.onload=function(){
        var aNodes=document.getElementsByTagName("a");
        for(var i=0;i<aNodes.length;i++){
            aNodes[i].onclick=function(){
            var request=new XMLHttpRequest();
            var method="GET";
            var url=this.href;
            request.open(method,url);
            request.send(null);
            request.onreadystatechange=function(){
                if(request.readyState==4){
                    if(request.status==200){
                        document.getElementById("zqwang121").innerHTML=request.responseText;
                    }
                }
            }
                return false;
            }
        }
    }
             
</script>

[代码]html

<body>
  <h1>People</h1>
  <ul>
      <li><a href="1.html">1</a></li>
      <li><a href="2.html">2</a></li>
      <li><a href="3.html">3</a></li>
  </ul>
  <div id="zqwang121"></div>
</body>
原文链接:https://www.f2er.com/ajax/161693.html

猜你在找的Ajax相关文章