Ajax使用示例

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

Index.php:
<script type="text/javascript" src="ajax.js" ></script>
<a href="#" οnclick="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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值