AJAX

什么是Ajax

Ajax=异步JavaScript和XML

XHR响应

 

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title>responsesXML属性</title>
 6         <script>
 7             function loadXMLDoc(){
 8                 var xmlhttp;
 9                 var txt,x,i;
10                 if(window.XMLHttpRequest){
11                     xmlhttp=new XMLHttpRequest();
12                 }
13                 else{
14                     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
15                 }
16                 xmlhttp.onreadystatechange=function(){
17                     if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
18                         xmlDoc=xmlhttp.responseXML;
19                         console.log(xmlDoc)
20                         txt='';
21                         x=xmlDoc.getElementsByTagName('ARTIST');
22                         for(i=0;i<x.length;i++){
23                             txt=txt+x[i].childNodes[0].nodeValue+"<br>"
24                         }
25                         console.log(txt)
26                         document.getElementById("myDiv").innerHTML=txt;
27                     }
28                 }
29                 xmlhttp.open("GET","https://www.runoob.com/try/demo_source/cd_catalog.xml", true);
30                 xmlhttp.send();
31             }
32         </script>
33     </head>
34     <body>
35         <h2>我收藏的CD</h2>
36         <div id="myDiv"></div>
37         <button type="button" onclick="loadXMLDoc()">获取我的CD</button>
38     </body>
39 </html>

 

posted @ 2023-02-01 21:04  H年轻的心  阅读(16)  评论(0)    收藏  举报