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 Examples
AJAX范例

AJAX - The XMLHttpRequest Object

AJAX-XMLHttpRequest对象

Previous Next

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()
{
// We are going to write some code here

//我们将在此写入一些处理代码
}



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()
{
if(xmlhttp.readyState==4)
 {
 // Get data from the server's response

 //从服务器响应中获取数据
 }
}



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!

下一章节我们将展示如何向服务器索要数据!


Previous Next

宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有