`
renzhelife
  • 浏览: 672645 次
文章分类
社区版块
存档分类
最新评论

Ajax使用示例

 
阅读更多

/***************************by garcon1986********************************/

Index.php:
<script type="text/javascript" src="ajax.js" ></script>
<a href="#" onclick="funphp('S')" >S</a>
<a href="for.php?url=E" >E</a>
<div id="idiv"></div>
for.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);
if($url = $_GET[url]){
for($i=1;$i<10;$i++){
echo $url;
}
exit();
}
?>
Ajax.js:
//首先声明变量
var xmlHttp;
//创建对象函数
function GetXmlHttpObject(){
try{
//Firefox, Opera, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e){
try{
//MS IE 6.0+
xmlHttp = new ActiveXObject(Msxml2.XMLHTTP);
}
catch(e){
try{
//MS IS 5.5+
xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
}
catch(e){
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
}
//发送请求函数
function funphp(t){
//调用GetXmlHttpObject函数
GetXmlHttpObject();
//open() 方法需要三个参数。第一个参数定义发送请求所使用的方法(GET 还是 POST)。第二个参数规定服务器端脚本的 URL。第三个方法规定应当对请求进行异步地处理。
xmlHttp.open("GET","for.php?url="+t, true);
//指定响应函数
xmlHttp.onreadystatechange = effect;
//发送请求。当使用GET方法时,一般send里的参数是null。如果是POST会有所不同。
xmlHttp.send(null);
}
//处理返回信息函数
function effect(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var cao = xmlHttp.responseText;
document.getElementById('idiv').innerHTML = cao;
}
}
}

注释:


xmlHttpRequest.readyState的5种状态:


0 (Uninitialized)
The object has been created, but not initialized (the open method has not been called).
1 (Open)
The object has been created, but the send method has not been called.
2 (Sent)
The send method has been called. responseText is not available. responseBody is not available.
3 (Receiving)
Some data has been received. responseText is not available. responseBody is not available.
4 (Loaded)
All the data has been received. responseText is available. responseBody is available.

The property is read-only. The property has no default value.



0:请求未初始化,对象已建立,还没有调用 open()。

1:请求已经建立,但是还没有发送,还没有调用 send()。

2:请求已发送,已调用send()方法,正在处理中(通常现在可以从响应中获取内容头)。

3:请求在处理中;通常响应中已有部分数据可用了,没有全部完成。

4:响应已完成;您可以通过responseBody,responseText获取并使用服务器的响应了。

w3c.org的定义:

The XMLHttpRequest object can be in several states. The readyState attribute, on getting, must return the current state, which must be one of the following values:

UNSENT (numeric value 0)
The object has been constructed.

OPENED (numeric value 1)
The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.

HEADERS_RECEIVED (numeric value 2)
All HTTP headers have been received. Several response members of the object are now available.
LOADING (numeric value 3)
The response entity body is being received.

DONE (numeric value 4)
The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).
The OPENED state has an associated send() flag that indicates whether the send() method has been invoked. It can be either true or false and has an initial value of false.
The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics