AJAX Basic
AJAX基础
AJAX HOME
AJAX首页
AJAX Intro
AJAX简介
AJAX Request
AJAX请求
AJAX Example
AJAX实例
AJAX Browsers
AJAX浏览器
AJAX XMLHttpRequest
AJAX XMLHttpRequest对象
AJAX Server
AJAX服务器端
AJAX Server Script
AJAX服务器脚本
AJAX Advanced
AJAX高阶
AJAX Suggest
AJAX建议
AJAX Source
AJAX源码
AJAX Database
AJAX数据库
AJAX XML File
AJAX XML文件
AJAX ResponseXML
AJAX响应XML
AJAX Examples
AJAX范例
AJAX - The XMLHttpRequest Object
AJAX-XMLHttpRequest对象
AJAX - More about the XMLHttpRequest object
AJAX-更多关于XMLHttpRequest对象
Before sending data off to a server, we will look at three important properties of the XMLHttpRequest object.
在给服务器发送数据之前,我们来看XMLHttpRequest对象的3个重要属性。
The onreadystatechange property
onreadystatechange属性
After a request to a server, we need a function to receive the data returned from the server.
当请求服务器之后,我们需要一个函数去接受服务器返回的数据。
The onreadystatechange property stores the function that will process the response from a server. The function is stored in the property to be called automatically.
onreadystatechange属性存储函数将处理服务器端的响应。属性存储函数自动被唤醒存入相应属性。
The following code sets the onreadystatechange property and stores an empty function inside it:
如下代码设置了onreadystatechange属性并将空函数值存入之:
xmlhttp.onreadystatechange=function() //我们将在此写入一些处理代码 |
The readyState property
readyState属性
The readyState property holds the status of the server's response.
readyState属性持有服务器返回状态。
Each time the readyState property changes, the onreadystatechange function will be executed.
每当readyState属性改变,onreadystatechange函数就会被执行。
Possible values for the readyState property:
readyState属性的可能值:
| State | Description |
|---|---|
| 0 | The request is not initialized |
| 1 | The request has been set up |
| 2 | The request has been sent |
| 3 | The request is in process |
| 4 | The request is complete |
| 状态值 | 描述 |
|---|---|
| 0 | 请求未初始化 |
| 1 | 请求已经建立 |
| 2 | 请求已经发送 |
| 3 | 请求中 |
| 4 | 请求已完成 |
Add an If statement to the onreadystatechange function to test if the response is complete (means that now we can get our data):
增加一个If语言去触发onreadystatechange函数去测试响应是否已经完成(意味着我们现在已经可以得到我们的数据了):
xmlhttp.onreadystatechange=function() //从服务器响应中获取数据 |
The responseText property
responseText属性(响应文本属性)
The data sent back from a server can be retrieved with the responseText property.
responseText属性能够回收服务器端返回的数据。
Now, we want to set the value of the "Time" input field equal to responseText:
现在,我们将给“Time”赋值为输入框中返回文本(responseText)
| xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { document.myForm.time.value=xmlhttp.responseText; } } |
The next chapter shows how to ask a server for data!
下一章节我们将展示如何向服务器索要数据!
