介绍一个AJAX的例子,分两个文件(当然要先配置好PHP环境)
以下是调用页checkUrl.php:
<?php header("expires:mon,26jul199705:00:00gmt"); //用于清楚缓存 header("cache-control:no-cache,must-revalidate"); //用于清楚缓存 header("pragma:no-cache"); //用于清楚缓存 $url=$_GET['url']; $arr=array( 'id'=>"http://"+$_GET['id'], 'url'=>$url ); $Json=json_encode($arr); echo $Json ?>
以下是index.html文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生勤工助学中心网站-广告管理</title>
</head>
<script type="text/javascript" language="javascript">
var ajax;
var as;
function getajax(){
try{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
as = 1;
}catch(e){
try{
ajax = new ActiveXObject("Msxml2.XMLHTTP");
as = 1;
}catch(e){
try{
ajax = new XMLHttpRequest();
as = 2;
}catch(e){
ajax = null;
as = 0;
}
}
}
}
function isExist(method, url, pars, id){
getajax();
url2='checkUrl.php?url='+url+'&id='+id+'&rand='+new Date().getTime();//随机数用于清楚调用缓存
if(as == 0){
alert("您的浏览器不支持XMLHTTP,无法完成此操作");
}else{
ajax.open(method,url2, false);
if(method == "POST"){
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}else{
ajax.setRequestHeader("Content-Type", "text/xml;charset=GB2312");
}
if(as == 1){
ajax.onreadystatechange = doShow;
}else{
ajax.onload = doShow;
ajax.onerror = doShow;
}
ajax.send(pars);
}
}
function doShow(){
if(ajax.readyState == 4){
json=eval('('+ajax.responseText+')');
alert(json.id+"的主页是:"+json.url);
}
}
</script>
</head>
<body>
<a onclick="isExist('get','xiebiji.com','a=2', 'joe')">看看JOE的主页是什么?</a>
</body>
</html> |
点击“看看JOE的主页是什么?”将提示“JOE的主页是:http://xiebiji.com”
这是AJAX模型的最基础模型
不错, 看来 博主开始努力学习了债主
回复
回复
不错, 看来 博主开始努力学习了
回复