XML HTTP 请求

本文介绍如何使用IE5.0及以上版本通过HTTP请求从服务器获取XML数据,并展示了利用JavaScript及VBScript实现这一过程的示例代码。此外,还提供了一个简单的搜索功能示例,演示了客户端如何发送查询到服务器并接收XML格式的回答。
摘要由CSDN通过智能技术生成

如果使用IE5.0或者更高版本的浏览器,可以通过HTTP请求从服务器端获取XML数据。


浏览器请求

通过浏览器可以发送HTTP请求,从服务器端获取XML数据:

var objHTTP = new ActiveXObject("Microsoft.XMLHTTP")
objHTTP.Open('GET','httprequest.asp',false)
objHTTP.Send()

下面的代码是在浏览器中显示从服务器端的得到的返回数据:

document.all['A1'].innerText= objHTTP.status
document.all['A2'].innerText= objHTTP.statusText
document.all['A3'].innerText= objHTTP.responseText

使用JavaScript脚本

 

< html >
< head >
< script  type ="text/javascript" >
function getPage()
{
var objHTTP = new ActiveXObject("Microsoft.XMLHTTP")
objHTTP.Open(
'GET','note.xml',false)
objHTTP.Send()
document.all[
'A1'].innerText= objHTTP.status
document.all[
'A2'].innerText= objHTTP.statusText
document.all[
'A3'].innerText= objHTTP.responseText
}

</ script >
</ head >
< body  onload ="getPage()" >
< h2 > Using the HttpRequest Object </ h2 >
< p >
< b > status: </ b >
< span  ID ="A1" ></ span >
</ p >
< p >
< b > status text: </ b >
< span  ID ="A2" ></ span >
</ p >
< p >
< b > response: </ b >
< br >< span  ID ="A3" ></ span >
</ p >
</ body >
</ html >


 

使用VBScript脚本

 

< html >
< head >
< script  type ="text/vbscript" >
function getPage()
set http_obj
=createObject("Microsoft.XMLHTTP")
call http_obj.Open(
"GET","note.xml",false)
call http_obj.Send()
a1.innerText 
= http_obj.status
a2.innerText 
= http_obj.statusText
a3.innerText 
= http_obj.responseText
end 
function
</ script >
</ head >
< body  onload ="getPage()" >
< h2 > Using the HttpRequest Object </ h2 >
< p >
< b > status: </ b >
< span  ID ="a1" ></ span >
</ p >
< p >
< b > status text: </ b >
< span  ID ="a2" ></ span >
</ p >
< p >
< b > response: </ b >
< br  />< span  ID ="a3" ></ span >
</ p >
</ body >
</ html >


与服务器的交互

通过HTTP请求就可以做到与服务器进行“交互”。

使用XML与服务器进行交互

 

< html >
< head >
< script  type ="text/javascript" >
function search()
{
var parser=new ActiveXObject("microsoft.xmldom")
parser.async
="false"
parser.load(
"tryxml_send.asp?query=" + query.value)
nodes 
= parser.documentElement.childNodes
answer_text.innerHTML
=nodes.item(0).text
answer_xml.value
=parser.xml
}

</ script >
</ head >
< body >
< h2 >
Sending a query to the server
and receiving the answer as XML:
</ h2 >
< p >
< b > Query:  </ b >
< input  type ="text"
name
="query"  value ="How Old" >
< input  type ="button"
value
="Send to Server"  onClick ="search()" >
</ p >
< p >
< b > Answer from server is: </ b >
< span  id ="answer_text" ></ span >
</ p >
< p >
< b > XML from server is: </ b >< br >
< textarea  id ="answer_xml"
cols
="80"  lines ="10"  rows ="1" >

 

我们在服务器端“捏造”了下面的ASP代码进行交互操作:

<%
response.ContentType="text/xml"
txt="<answer><text>12 Years</text></answer>"
response.write(txt)
%>

因此,无论你提问什么,回答永远是12年。在现实生活中,不得不去写大量的代码去分析问题并且响应出正确的答案。


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值