当前位置: 首页 > 网络学院 > XML相关教程 > XML > XMLHttpRequest 对象

XML
XML DHTML行为
XML 相关技术
XML 编辑器
XML 摘要
XML 实例
XML字符编码
xml 文档树
IE和火狐读取XML方法比较

XML 中的 XMLHttpRequest 对象


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 680 ::
收藏到网摘: n/a

The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 9, and Netscape 7.
XMLHttpRequest 对象可被 Internet Explorer 5.0+、Safari 1.2、Mozilla 1.0 / Firefox、Opera 9 和 Netscape 7.支持


What is an HTTP Request?
什么是HTTP 请求?

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.
通过HTTP请求,页面可以直接向服务器发出请求,从而获取来自服务器端的回应,而不须要再重新加载页面。使用者仍然会停留在相同的页面上,他/她并不会意识到脚本程序已经私下里向服务器发出请求或发送数据了。

By using the XMLHttpRequest object, a web developer can change a page with data from the server after the page has loaded.
通过使用XMLHttpRequest 对象,网站开发者在加载页面后可以获取来自服务器的数据来更新页面。

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.
Google Suggest 是使用XMLHttpRequest 象来创建一个极具动态效果的网页界面:当你在Google的搜索框内输入搜索信息时,一份Javascript 脚本程序会将字母发送给服务器,而服务器则返回一系列建议。


Is the XMLHttpRequest Object a W3C Standard?
XMLHttpRequest对象是W3C标准吗?

The XMLHttpRequest object is not a W3C standard.
XMLHttpRequest 对象不是W3C标准。

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.
“W3C DOM Level 3” 的“下载和保存”规范包含了一些相似的功能,但这些功能没有在任何浏览器中执行。因此现在,如果你想要从浏览器端发送一份HTTP要求,你就必须使用XMLHttpRequest对象。


Creating an XMLHttpRequest Object
创建一个XMLHttpRequest对象

For Mozilla, Firefox, Safari, Opera, and Netscape:
为 Mozilla、, Firefox、Safari、Opera 和 Netscape 创建一个 XMLHttpRequest 对象。

var xmlhttp=new XMLHttpRequest()

For Internet Explorer:
为IE创建:

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

Example
案例

<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
// code for 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("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
// 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
尝试使用JavaScript 创建

The syntax is a little bit different in VBScript: Try it yourself using VBScript
在语法上VBscript会有点不同:尝试使用 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.
注意:
以上例子的重要特性是“onreadystatechange[ 随时状态可变 ]” 属性。该属性是一个“事件处理者”,每当请求状态改变时,它都会被触发。状态从0(初始化)到4(完成)。通过xmlhttpChange() 功能对状态改变的检查,我们能够区分程序是否结束,或将继续。


 

Why are we Using Async in our Examples?
为什么我们要在案例中使用Async?

All the examples here use the async mode (the third parameter of open() set to true).
这儿所有例子都用了async 模式(open() 元素的第三个参数被设置为“真”)

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!
Async 参数指定了发出的请求是否需要被同时处理。“真(TRUE)”表示:在send() 方法被执行后,脚本程序继续运行,不等待服务器端的回应。“假(FALSE)”表示:脚本程序继续运行前须要等待服务器端的回应。将设置参数为假,如果网络或服务器有问题,或者请求时间过长(发出请求时UI锁定),你的脚本程序就会被挂起,使用者可能会看到“没有回应”的信息。更加安全的做法是:同步发送信息;或者,在“onreadystatechange[ 随时状态可变 ]” 事件下设计代码。


 

More Examples
更多举例

通过XML HTTP(JavaScript)将文本文件载入一个div元素

通过XML HTTP(JavaScript)发出一个Head请求

通过XML HTTP(JavaScript)发出一个特定的Head请求

通过XML HTTP(JavaScript)列出一个XML文件中的数据


The XMLHttpRequest Object Reference
XMLHttpRequest 对象参数

Methods
方法

方法 描述
abort() Cancels the current request
取消当前请求
getAllResponseHeaders() Returns the complete set of http headers as a string
以字符串形式返回完整的http headers属性
getResponseHeader("headername") Returns the value of the specified http header
返回指定的http header值
open("method","URL",async,"uname","pswd") Specifies the method, URL, and other optional attributes of a request
指定方法、URL(超链接)以及其它与请求相关的附加信息

 

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

The URL parameter may be either a relative or complete URL.
URL参数可以是整条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
“Async”参数指定了发送的请求是否需要同步处理。“True:真”表示:脚本程序在send()方法执行后,无须等待服务器回应便可以继续处理;而“False:假”则表示:脚本程序在处理之前须要等待服务器端的回应

send(content) Sends the request
发送请求
setRequestHeader("label","value") Adds a label/value pair to the http header to be sent
将“标签(label)/ 值(value)”成对添加到将要被发送的“http header”中

Properties
属性

属性 描述
onreadystatechange An event handler for an event that fires at every state change
一个能够激发所有状态改变的事件处理器
readyState Returns the state of the object:
返回对象的具体状态

0 = uninitialized
0 = 未被初始化

1 = loading
1 = 正在加载的

2 = loaded
2 = 已被加载的

3 = interactive
3 = 交互式的

4 = complete
4 = 已完成的

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
以XML的形式返回响应。这个属性返回了一个XML文档对象,该对象可以通过W3C DOM网络节点树的方法和属性来检验和解析。
status Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")
返回以数字形式表示的状态 ( 如:404表示"Not Found:未找到"; 200 为 "OK:通过" )
statusText Returns the status as a string (e.g. "Not Found" or "OK")
返回以字符串形式表示的状态 (如: "Not Found:未找到" 或 "OK:通过")

评论 (0) All

登陆 | 还没注册?