The XMLHttpRequest object makes AJAX possible.
The XMLHttpRequest
To create AJAX web applications you have to become familiar with the JavaScript object called the XMLHttpRequest.
要建立AJAXweb应用程序你就必须开始熟悉JS对象中的XMLHttpRequest
The XMLHttpRequest object is the key to AJAX. It has been available ever since Internet Explorer 5.5 was released in July 2000, but not fully discovered before people started to talk about AJAX and Web 2.0 in 2005.
XMLHttpRequest 对象是AJAX的关键。它其实早在2000年的七月便开始生效了,但直到2005年人们开始谈论AJAX和Web2.0后才被发掘。
Below is listed some of the methods and properties you have to become familiar with.
下面列举的是一些方法和属性,这些你都应该非常熟悉
建立XMLHttpRequest对象
Different browsers use different methods to create an XMLHttpRequest object.
不同的浏览器使用了不同的建立方法
Internet Explorer uses an ActiveXObject.
在IE里使用的是ActiveXObject
Other browsers uses a built in JavaScript object called XMLHttpRequest.
其它浏览器使用的是JS内建的XMLHttpRequest 对象
Here is the simplest code you can use overcome this problem:
这是段简单的代码,可以用来解决这个问题:
var XMLHttp=null if (window.XMLHttpRequest) { XMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { XMLHttp=new ActiveXObject("Microsoft.XMLHTTP") }
|
Example above explained:
上面举例的解析:
First create a variable XMLHttp to use as your XMLHttpRequest object. Set the value to null.
首先建立的是一个名为XMLHttp的变量,我们可以用它来代替XMLHttpRequest对象。设置这个变量的值为null
Then test if the object window.XMLHttpRequest is available. This object is available in newer versions of browsers like Firefox, Mozilla, and Opera.
然后测试下对象window.XMLHttpRequest是否有效。这个对象只在Firefox, Mozilla, 和 Opera这些浏览器的新版本中有效。
If it's available, use it to create a new object:
如果它是有效的,那么就使用它来建立一个新的对象:
XMLHttp=new XMLHttpRequest().
If it's not available, test if an object window.ActiveXObject is available. This object is available in Internet Explorer version 5.5 and later.
如果无效。测试下对象window.ActiveXObject是否有效。这个对象只在IE5.5以上的版本中有效。
If it is available, use it to create a new object:
如果有效,那么建立一个新的对象:
XMLHttp=new ActiveXObject().
更好一点的举例?
Some programmers will prefer to use the newest and fastest version of the XMLHttpRequest object.
一些程序希望使用最新最快版本的XMLHttpRequest对象。
The example below tries to load Microsoft's the latest version "Msxml2.XMLHTTP", available in Internet Explorer 6, before it falls back to "Microsoft.XMLHTTP", available in Internet Explorer 5.5 and later.
下面这个举例会尝试加载微软最新版本中的"Msxml2.XMLHTTP"。它在IE6中有效,以前的版本都是"Microsoft.XMLHTTP"。
var XMLHttp=null try { XMLHttp=new ActiveXObject("Msxml2.XMLHTTP") } catch(e) { try { XMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } }
if (XMLHttp==null) { XMLHttp=new XMLHttpRequest() }
|
Example above explained:
上面举例的解析:
First create a variable XMLHttp to use as your XMLHttpRequest object. Set the value to null.
首先建立一个XMLHttp变量可用做你的XMLHttpRequest对象。设置为null值
Then try to create the object the Microsoft way, available in Internet Explorer 6 and later:
然后尝试用MS的方法来建立一个在IE6或更新版本中有效的对象:
XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")
If this catches an error, try the older (Internet Explorer 5.5) way:
如果捕获到了错误,就尝试用老一点的方法:
XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
If XMLHttp still has a null value, try to create the object the "standard" way:
如果XMLHttp依旧为null值,那么就用“标准”方法建立:
XMLHttp=new XMLHttpRequest()
XMLHttpRequest 方法
open() 方法.
The open() method sets up a request to a web server.
建立对web服务器的请求。
send() 方法.
The send() method sends a request to the server.
向服务器发送请求
abort() 方法.
The abort() method aborts the current server request.
放弃当前的服务器请求
XMLHttpRequest readyState 属性
The readyState property defines the current state of the XMLHttpRequest object.
readyState 定义了当前XMLHttpRequest对象的状态
Here are the possible values for the readyState propery:
这所列举的是一些readyState可能出现的值以及相应的描述:
State 状态 |
描述 |
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 completed 请求完成 |
readyState=0 after you have created the XMLHttpRequest object, but before you have called the open() method.
发生在你已经建立XMLHttpRequest对象之后,而在你调用open()方法之前的这个阶段
readyState=1 after you have called the open() method, but before you have called send().
发生在你已经调用open()方法之后,而在你调用send()方法之前的这个阶段
readyState=2 after you have called send().
发生在你已经调用send()方法之后
readyState=3 after the browser has established a communication with the server, but before the server has completed the response.
发生在浏览器已经于服务器建立沟通之后,而在服务器完成反馈前的这个阶段
readyState=4 after the request has been completed, and the response data have been completely received from the server.
发生在请求完成并收到来自服务器的反馈信息之后
Different browsers treat the ready state differently. Don't expect all browsers to report all states. Some will not report 0 and 1.
不同的浏览器对于不同的状态处理方式不同。别指望所有浏览器都能报告所有状态。一些浏览器不会报告0和1
For Your AJAX applications you will actually only be interested state 4. That is when the request is completed and it is safe use the received data.
在你自己的AJAX程序中事实上你只要注重下状态4就行了。
XMLHttpRequest responseText 属性
The responseText property contains the text returned by the server.
responseText属性包含了从服务器返回的文字信息