王朝网络
分享
 
 
 

[收藏]The XMLHttpRequest Object

王朝other·作者佚名  2006-02-01
宽屏版  字体: |||超大  

作为Ajax三条腿之一的XMLHttpRequest是比较重要的工具,这里是w3shcools提供的比较官方的文档,供大家参考。比较重要的是最后的The XMLHttpRequest Object Reference,这个可以用来做手册用。

The XMLHttpRequest Object

The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, and Netscape 7.

What is an HTTP Request?

With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts might request pages, or send data to a server in the background.

By using the XMLHttpRequest object, a web developer can change a page with data from the server after the page has loaded.

Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.

Is the XMLHttpRequest Object a W3C Standard?

The XMLHttpRequest object is not a W3C standard.

The W3C DOM Level 3 "Load and Save" specification contains some similar functionality, but these are not implemented in any browsers yet. So, at the moment, if you need to send an HTTP request from a browser, you will have to use the XMLHttpRequest object.

Creating an XMLHttpRequest Object

For Mozilla, Firefox, Safari, and Netscape:

var xmlhttp=new XMLHttpRequest()

For Internet Explorer:

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

Example

<script type="text/javascript">

var xmlhttp

function loadXMLDoc(url)

{

// code for Mozilla, etc.

if (window.XMLHttpRequest)

{

xmlhttp=new XMLHttpRequest()

xmlhttp.onreadystatechange=xmlhttpChange

xmlhttp.open("GET",url,true)

xmlhttp.send(null)

}

// code for IE

else if (window.ActiveXObject)

{

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

if (xmlhttp)

{

xmlhttp.onreadystatechange=xmlhttpChange

xmlhttp.open("GET",url,true)

xmlhttp.send()

}

}

}

function xmlhttpChange()

{

// if xmlhttp shows "loaded"

if (xmlhttp.readyState==4)

{

// if "OK"

if (xmlhttp.status==200)

{

// ...some code here...

}

else

{

alert("Problem retrieving XML data")

}

}

}

</script>

Try it yourself using JavaScript

The syntax is a little bit different in VBScript: Try it yourself using VBScript

Note: An important property in the example above is the onreadystatechange property. This property is an event handler which is triggered each time the state of the request changes. The states run from 0 (uninitialized) to 4 (complete). By having the function xmlhttpChange() check for the state changing, we can tell when the process is complete and continue only if it has been successful.

Why are we Using async in our Examples?

All the examples here use the async mode (the third parameter of open() set to true).

The async parameter specifies whether the request should be handled asynchronously or not. True means that script continues to run after the send() method, without waiting for a response from the server. false means that the script waits for a response before continuing script processing. By setting this parameter to false, you run the risk of having your script hang if there is a network or server problem, or if the request is long (the UI locks while the request is being made) a user may even see the "Not Responding" message. It is safer to send asynchronously and design your code around the onreadystatechange event!

More Examples

Load a textfile into a div element with XML HTTP (JavaScript)

Make a HEAD request with XML HTTP (JavaScript)

Make a specified HEAD request with XML HTTP (JavaScript)

The XMLHttpRequest Object Reference

Methods

Method

Description

abort()

Cancels the current request

getAllResponseHeaders()

Returns the complete set of http headers as a string

getResponseHeader("headername")

Returns the value of the specified http header

open("method","URL",async,"uname","pswd")

Specifies the method, URL, and other optional attributes of a request

The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes.

The URL parameter may be either a relative or complete URL.

The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing

send(content)

Sends the request

setRequestHeader("label","value")

Adds a label/value pair to the http header to be sent

Properties

Property

Description

onreadystatechange

An event handler for an event that fires at every state change

readyState

Returns the state of the object:

0 = uninitialized

1 = loading

2 = loaded

3 = interactive

4 = complete

responseText

Returns the response as a string

responseXML

Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties

status

Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")

statusText

Returns the status as a string (e.g. "Not Found" or "OK")

best regards!

coofucoo zhang(戒盈祈願)

The stone has started rolling. It will became a great mountain and fill the whole earth.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有