The XMLHttpRequest 对象
使用XMLHttpRequest对象,您可以更新web页面的部分内容,而无需重新加载整个页面。
更多实例
A
simple XMLHttpRequest example
创建一个简单的XMLHttpRequest,并从TXT文件中检索数据。
Retrieve header information with getAllResponseHeaders()
检索资源(文件)的标题信息。
Retrieve specific header information with getResponseHeader()
检索资源(文件)的特定头信息。
Retrieve the content of an ASP file
当用户在输入字段中键入字符时,网页如何与 web 服务器通信。
Retrieve content from a database
网页如何使用 XMLHttpRequest 对象从数据库获取信息。
Retrieve the content of an XML file
创建 XMLHttpRequest 以从 XML 文件检索数据并在 HTML 中显示数据。
XMLHttpRequest 对象
XMLHttpRequest 对象用于在后台与服务器交换数据。
XMLHttpRequest 对象是开发人员的梦想,因为您可以:
- 在不重新加载网页的情况下更新网页
- 加载页面后从服务器请求数据
- 加载页面后从服务器接收数据
- 在后台向服务器发送数据
XMLHttpRequest 对象方法
方法 | 描述 |
---|---|
abort() | 取消当前的请求。 |
getAllResponseHeaders() | 返回头信息。 |
getResponseHeader() | 返回指定的头信息。 |
open(method,url,async,uname,pswd) | 初始化 HTTP 请求参数,指定请求的类型、URL、是否应异步处理请求以及请求的其他可选属性
method: 请求的类型:GET 或 POST url: 文件在服务器上的位置 async: true(异步)或 false(同步) |
send(string) | 发送 HTTP 请求,使用传递给 open() 方法的参数,以及传递给该方法的可选请求体。br> string: 仅用于POST请求 |
setRequestHeader() | 将标签/值对添加到要发送的标头 |
XMLHttpRequest 对象属性
属性 | 描述 |
---|---|
onreadystatechange | 存储函数(或函数的名称)在每次 readyState 属性变化时被自动调用。 |
readyState | 存放了 XMLHttpRequest 的状态。从 0 到 4 变化: 0: 请求未初始化 1: 服务器建立连接 2: 收到的请求 3: 处理请求 4: 请求完成和响应准备就绪 |
responseText | 返回作为一个字符串的响应数据。 |
responseXML | 返回作为 XML 数据响应数据。 |
status | 返回状态数 例如 "404" for "Not Found" or "200" for "OK") |
statusText | 返回状态文本例如 "Not Found" or "OK") |