目录

XMLHttpRequest 对象


使用 XMLHttpRequest 对象,您可以更新网页的部分内容,而无需重新加载整个页面。


XMLHttpRequest 对象

XMLHttpRequest 对象用于与后台服务器交换数据。

XMLHttpRequest 对象是开发商的梦想, 因为你能:

  • 更新网页而不重新加载页面
  • 页面加载后向服务器请求数据
  • 页面加载后从服务器接收数据
  • 在后台将数据发送到服务器


XMLHttpRequest 对象方法

Method Description
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method,url,async,uname,pswd) Specifies the type of request, the URL, if the request should be handled asynchronously or not, and other optional attributes of a request

method: the type of request: GET or POST
url: the location of the file on the server
async: true (asynchronous) or false (synchronous)
send(string) send(string) Sends the request off to the server.

string: Only used for POST requests
setRequestHeader() Adds a label/value pair to the header to be sent

XMLHttpRequest 对象属性

Property Description
onreadystatechange Stores a function (or the name of a function) to be called automatically each time the readyState property changes
readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
responseText Returns the response data as a string
responseXML Returns the response data as XML data
status Returns the status-number (e.g. "404" for "Not Found" or "200" for "OK")
statusText Returns the status-text (e.g. "Not Found" or "OK")