当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM HttpRequest

XML DOM
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError
DOM 校验器
DOM 介绍
DOM 摘要
DOM 案例
DOM 节点树
DOM 访问节点树
DOM 节点信息
DOM 文档执行
DOM 节点导航

XML DOM 中的 DOM HttpRequest


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

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


What is an HTTP Request?
什么是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.
通过一个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(Google 建议)是通过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标准3 "Load and Save"(加载和保存)的详细说明中包含了一些类似的功能,但它们还不能在浏览器中执行。因此,如果你现在就希望从浏览器中发送一个HTTP请求,就需要使用XMLHttpRequest对象。


Creating an XMLHttpRequest Object
创建XMLHttpRequest对象

For Mozilla, Firefox, Safari, Opera, and Netscape:
对于Mozilla、Firefox、Safari、Opera 和 Netscape:

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
自己尝试一下吧!

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?
我们为何在案例中使用同步?

Most of the examples here use the async mode (the third parameter of open() set to true).
这儿大部分例子都用到了同步模式(当第三个参数open()设置为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!
async参数详细指明了请求是否应该被同步处理。True表示在send()函数运行之后,不等待服务器的响应。False表示在脚本程序继续处理之前,等待服务器的响应。如果你把参数设置成false,在网络服务器出现问题或请求时间过长(当处于请求过程中时UI已被锁住)时,用户甚至会收到“无响应(Not Responding)” 指示。上述过程对于异步传输以及onreadystatechange事件的编码设计来说都是非常安全的!


More Examples
更多案例

Load a textfile into a div element with XML HTTP (JavaScript)
通过HTTP(JavaScript)将一个文本文件加载到DIA元素

Make a HEAD request with XML HTTP (JavaScript)
通过XML HTTP(JavaScript)提出一个HEAD请求

Make a specified HEAD request with XML HTTP (JavaScript)
通过XML HTTP(JavaScript)提出一个HEAD请求

List data from an XML file with XML HTTP (JavaScript)
通过XML HTTP(JavaScript)列出XML文件中的数据


XML / ASP

You can also open and send an XML document to an ASP page on the server, analyze the request, and send back the result.
你也可以打开XML文件并将它发送到服务器上的一个ASP页面,然后分析请求并传回结果:

<html>
<body>
<script type="text/javascript">
var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
xmlHttp.open("GET", "note.xml", false)
xmlHttp.send()
xmlDoc=xmlHttp.responseText
xmlHttp.open("POST", "demo_dom_http.asp", false)
xmlHttp.send(xmlDoc)
document.write(xmlHttp.responseText)

</script>
</body>
</html>

The ASP page, written in VBScript:
用VB脚本语言编写的ASP页面:

<%
set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.async=false
xmldoc.load(request)

for each x in xmldoc.documentElement.childNodes
if x.NodeName = "to" then name=x.text
next
response.write(name)
%>

You send the result back to the client using the response.write property.
使用response.write属性把结果传回客户端:

For Internet Explorer 5.0+: Try it yourself
IE5.0:你可以自己试一试^_^


The XMLHttpRequest Object Reference
XMLHttpRequest 对象参数

Methods方法

Method
方法
Description
描述
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 headers值
open("method","URL",async,"uname","pswd") Specifies the method, URL, and other optional attributes of a request
指定方式(method)、URI以及其它可选择的请求属性

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" 值|请求数据用 "GET" ,发送数据(特别是数据长度大于等于512字节的情况下)用“POST”。

The URL parameter may be either a relative or complete 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属性

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
以XML形式响应。这个属性返回一个XML文件对象(它可以通过W3C DOM节点树德方法和属性进行检查和解析)
status Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")
以数字的形式返回一个状态(比如:"Not Found"(无法找到)就用404;“ok”就用200来表示)
statusText Returns the status as a string (e.g. "Not Found" or "OK")
以字符串形式返回状态(如:"Not Found" 、 "OK")

评论 (0) All

登陆 | 还没注册?