创建一个XMLHttpRequest对象(例子)

使用VBScript创建:
< html >
< head >
< script  type ="text/vbscript" >
dim xmlhttp

function  loadXMLDoc(url)
set xmlhttp
= createObject( " Microsoft.XMLHTTP " )
xmlhttp.onreadystatechange
= getRef( " state_Change " )
call xmlhttp.open(
" GET " ,url, true )
call xmlhttp.send()
end 
function

function  state_Change()
if  xmlhttp.readyState = 4  then
  
if  xmlhttp.status = 200  then
    alert(
" XML数据正常 " )
    document.getElementById(
" A1 " ).innerText = xmlhttp.status
    document.getElementById(
" A2 " ).innerText = xmlhttp.statusText
    document.getElementById(
" A3 " ).innerText = xmlhttp.responseText
  
else
    alert(
" XML检索结果: "   &  xmlhttp.statusText)
  end 
if
end 
if
end 
function

</ script >
</ head >

< body  onload ="loadXMLDoc('text/note.xml')" >
< h2 > Using the HttpRequest Object </ h2 >

< p >< b > 状态: </ b >
< span  id ="A1" ></ span >
</ p >

< p >< b > 状态描述: </ b >
< span  id ="A2" ></ span >
</ p >

< p >< b > 返回: </ b >
< br >< span  id ="A3" ></ span >
</ p >

</ body >
</ html >

使用JavaScript创建:
< html >
< head >
< script  type ="text/javascript" >
var  xmlhttp

function  loadXMLDoc(url)
{
xmlhttp
= null
//  Mozilla
if  (window.XMLHttpRequest)
  {
  xmlhttp
= new  XMLHttpRequest()
  }
//  IE
else   if  (window.ActiveXObject)
  {
  xmlhttp
= new  ActiveXObject( " Microsoft.XMLHTTP " )
  }
if  (xmlhttp != null )
  {
  xmlhttp.onreadystatechange
= state_Change
  xmlhttp.open(
" GET " ,url, true )
  xmlhttp.send(
null )
  }
else
  {
  alert(
" 你的浏览器不支持 XMLHTTP! " )
  }
}

function  state_Change()
{
//  xmlhttp 加载完成
if  (xmlhttp.readyState == 4 )
  {
  
//  处理成功
   if  (xmlhttp.status == 200 )
  {
  alert(
" XML数据处理成功! " )
  document.getElementById(
' A1 ' ).innerHTML = xmlhttp.status
  document.getElementById(
' A2 ' ).innerHTML = xmlhttp.statusText
  document.getElementById(
' A3 ' ).innerHTML = xmlhttp.responseText
  }
  
else
  {
  alert(
" XML检索结果: "   +  xmlhttp.statusText)
  }
  }
}

</ script >
</ head >

< body  onload ="loadXMLDoc('text/note.xml')" >
< h2 > 使用HttpRequest对象 </ h2 >

< p >< b > 状态: </ b >
< span  id ="A1" ></ span >
</ p >

< p >< b > 状态描述: </ b >
< span  id ="A2" ></ span >
</ p >

< p >< b > 返回: </ b >
< br >< span  id ="A3" ></ span >
</ p >

</ body >
</ html >

XMLHttpRequest对象的方法:
方法描述
abort()取消当前请求
getAllResponseHeaders()以字符串形式返回完整的http headers属性
getResponseHeader("headername")返回指定的http header值
open("method","URL",async,"uname","pswd")指定方法、URL(超链接)以及其它与请求相关的附加信息

方法参数可以获取“GET”、“POST”以及“PUT”的值;如果发送的数据长度大于512字节,那么请使用“POST”命令。

URL参数可以是整条URL的信息或者是其中相关的一部份

“Async”参数指定了发送的请求是否需要同步处理。“True:真”表示:脚本程序在send()方法执行后,无须等待服务器回应便可以继续处理;而“False:假”则表示:脚本程序在处理之前须要等待服务器端的回应

send(content)发送请求
setRequestHeader("label","value")将“标签(label)/ 值(value)”成对添加到将要被发送的“http header”中

XMLHttpRequest对象的属性:
属性描述
onreadystatechange一个能够激发所有状态改变的事件处理器
readyState返回对象的具体状态

0 = uninitialized
0 = 未被初始化

1 = loading
1 = 正在加载的

2 = loaded
2 = 已被加载的

3 = interactive
3 = 交互式的

4 = complete
4 = 已完成的

responseText以字符串的形式返回响应
responseXML以XML的形式返回响应。这个属性返回了一个XML文档对象,该对象可以通过W3C DOM网络节点树的方法和属性来检验和解析。
status返回以数字形式表示的状态 ( 如:404表示"Not Found:未找到"; 200 为 "OK:通过" )
statusText返回以字符串形式表示的状态 (如: "Not Found:未找到" 或 "OK:通过")

转载于:https://www.cnblogs.com/Tuwi/archive/2008/05/08/1188824.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值