html 调用xml 类似 ajax

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head >
    
< title > Parsing XML Responses with the W3C DOM </ title >
< script  type ="text/javascript" >
var  xmlHttp;
var  requestType  =   "" ;

function  createXMLHttpRequest() {
    
if  (window.ActiveXObject) {
        xmlHttp 
=   new  ActiveXObject( " Microsoft.XMLHTTP " );
    } 
    
else   if  (window.XMLHttpRequest) {
        xmlHttp 
=   new  XMLHttpRequest();
    }
}
    
function  startRequest(requestedList) {
    requestType 
=  requestedList;
    
    createXMLHttpRequest();
    xmlHttp.onreadystatechange 
=  handleStateChange;
    xmlHttp.open(
" GET " " parseXML.xml " true );
    xmlHttp.send(
null );
}
    
function  handleStateChange() {

    
if (xmlHttp.readyState  ==   4 ) {
  
        
if (xmlHttp.status  ==   0 ) {
        
            
if (requestType  ==   " north " ) {
                listNorthStates();
            }
            
else   if (requestType  ==   " all " ) {
                listAllStates();
            }
        }
    }
}
 
function  listNorthStates() {
    
var  xmlDoc  =  xmlHttp.responseXML;
      
    
var  northNode  =  xmlDoc.getElementsByTagName( " north " )[ 0 ];
    
    
var  out  =   " Northern States " ;
    
var  northStates  =  northNode.getElementsByTagName( " state " );
    
    outputList(
" Northern States " , northStates);
}

function  listAllStates() {
    
var  xmlDoc  =  xmlHttp.responseXml;
    
    
var  allStates  =  xmlDoc.getElementsByTagName( " state " );
    
    outputList(
" All States in Document " , allStates);
}

function  outputList(title, states) {
    
var  out  =  title;
    
var  currentState  =   null ;
    
for ( var  i  =   0 ; i  <  states.length; i ++ ) {
        currentState 
=  states[i];
        out 
=  out  +   " "   +  currentState.childNodes[ 0 ].nodeValue;
    }
    
    alert(out);
}
</ script >
</ head >

< body >
    
< h1 > Process XML Document of U.S. States </ h1 >
    
< br />< br />
    
< form  action ="#" >
        
< input  type ="button"  value ="View All Listed States"  onclick ="startRequest('all');" />
        
< br />< br />
        
< input  type ="button"  value ="View All Listed Northern States"  onclick ="startRequest('north');" />
    
</ form >
</ body >
</ html >
< table  border ="1" >
  
< tbody >
    
< tr >
      
< th > Activity Name </ th >
      
< th > Location </ th >
      
< th > Time </ th >
    
</ tr >
    
< tr >
      
< td > Waterskiing </ td >
      
< td > Dock #1 </ td >
      
< td > 9:00 AM </ td >
    
</ tr >
    
< tr >
      
< td > Volleyball </ td >
      
< td > East Court </ td >
      
< td > 2:00 PM </ td >
    
</ tr >
    
< tr >
      
< td > Hiking </ td >
      
< td > Trail 3 </ td >
      
< td > 3:30 PM </ td >
    
</ tr >
  
</ tbody >
</ table >
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用AJAX(Asynchronous JavaScript and XML)来调用后端接口。AJAX是一种在不重新加载整个页面的情况下与服务器进行异步通信的技术。下面是一个简单的示例代码,展示如何使用AJAX调用后端接口(假设你的后端接口是一个GET请求): ```javascript // 创建一个XMLHttpRequest对象 var xhr = new XMLHttpRequest(); // 设置请求的类型和URL xhr.open('GET', 'http://your-backend-api-url', true); // 设置请求完成时的回调函数 xhr.onload = function() { if (xhr.status === 200) { // 请求成功,处理返回的数据 var response = JSON.parse(xhr.responseText); console.log(response); } else { // 请求失败,处理错误信息 console.error('请求失败:' + xhr.status); } }; // 发送请求 xhr.send(); ``` 在这个示例中,你需要将`http://your-backend-api-url`替换为你实际的后端接口URL。当AJAX请求完成时,可以通过`xhr.responseText`获取服务器返回的数据。 当然,如果你使用JavaScript库(如jQuery),也可以使用其提供的AJAX方法来简化代码。例如,在jQuery中,可以使用`$.ajax`方法来发送AJAX请求: ```javascript $.ajax({ url: 'http://your-backend-api-url', method: 'GET', success: function(response) { console.log(response); }, error: function(xhr, status, error) { console.error('请求失败:' + error); } }); ``` 这只是一种常见的方式,具体实现方法可能会根据你的后端框架和需求有所不同。希望对你有所帮助!如有更多问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值