AJAX在IE下调用的时候第一次发送了request,而且返回来的时间是对的,但是多次调用就不行, 时间一直是第一次调用后的值.问了原来的同事,才知道原来IE在很短时间访问同一地址时是从临时文件里读取的, 所以他返回的永远都是第一次调用的临时文件里的时间值. 所以每次发送URL时要保证URL是不一样的. 这样就没有缓存.
url + '?r='+Math.random() 加上一个随机数. r 变量随便自己定.
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
3
<head>
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
<title>无标题文档</title>
6
<script type="text/javascript" language="javascript">
7
8
var $ = function (id) {
9
return "string" == typeof id ? document.getElementById(id) : id;
10
};
11
12
function AJAXRequest() {
13
var xmlObj = false;
14
var CBfunc,ObjSelf;
15
ObjSelf=this;
16
try { xmlObj=new XMLHttpRequest; }
17
catch(e) {
18
try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); }
19
catch(e2) {
20
try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); }
21
catch(e3) { xmlObj=false; }
22
}
23
}
24
if (!xmlObj) return false;
25
this.method="POST";
26
this.url;
27
this.async=true;
28
this.content="";
29
this.callback=function(cbobj) {return;}
30
this.send=function() {
31
if(!this.method||!this.url||!this.async) return false;
32
if(this.url.indexOf('?')==-1){
33
this.url = this.url + '?r='+Math.random();
34
}else{
35
this.url = this.url + '&r='+Math.random();
36
}
37
38
xmlObj.open (this.method, this.url, this.async);
39
if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
40
xmlObj.onreadystatechange=function() {
41
if(xmlObj.readyState==4) {
42
if(xmlObj.status==200) {
43
ObjSelf.callback(xmlObj);
44
}
45
}
46
}
47
if(this.method=="POST") xmlObj.send(this.content);
48
else xmlObj.send(null);
49
}
50
}
51
52
var test =function(){
53
var ajaxobj=new AJAXRequest;
54
ajaxobj.method="GET";
55
ajaxobj.url="http://localhost/test/ajax_use.php"
56
57
ajaxobj.callback = function(xmlobj) {
58
$('result').innerHTML = xmlobj.responseText;
59
}
60
ajaxobj.send();
61
}
62
</script>
63
</head>
64
65
<body>
66
67
<textarea name="result" id="result" cols="45" rows="5" style="width:200px; height:50px; border:#F00 1px double"></textarea>
68
<!-- <div id="result"></div>-->
69
<p>
70
<input type="button" name="button" id="button" value="按钮" onclick="test();" />
71
</p>
72
</body>
73
</html>
74
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">2
<html xmlns="http://www.w3.org/1999/xhtml">3
<head>4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />5
<title>无标题文档</title>6
<script type="text/javascript" language="javascript">7

8
var $ = function (id) {9
return "string" == typeof id ? document.getElementById(id) : id;10
};11

12
function AJAXRequest() {13
var xmlObj = false;14
var CBfunc,ObjSelf;15
ObjSelf=this;16
try { xmlObj=new XMLHttpRequest; }17
catch(e) {18
try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); }19
catch(e2) {20
try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); }21
catch(e3) { xmlObj=false; }22
}23
}24
if (!xmlObj) return false;25
this.method="POST";26
this.url;27
this.async=true;28
this.content="";29
this.callback=function(cbobj) {return;}30
this.send=function() {31
if(!this.method||!this.url||!this.async) return false;32
if(this.url.indexOf('?')==-1){33
this.url = this.url + '?r='+Math.random();34
}else{35
this.url = this.url + '&r='+Math.random();36
}37

38
xmlObj.open (this.method, this.url, this.async);39
if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");40
xmlObj.onreadystatechange=function() {41
if(xmlObj.readyState==4) {42
if(xmlObj.status==200) {43
ObjSelf.callback(xmlObj);44
}45
}46
}47
if(this.method=="POST") xmlObj.send(this.content);48
else xmlObj.send(null);49
}50
}51

52
var test =function(){53
var ajaxobj=new AJAXRequest; 54
ajaxobj.method="GET"; 55
ajaxobj.url="http://localhost/test/ajax_use.php" 56

57
ajaxobj.callback = function(xmlobj) {58
$('result').innerHTML = xmlobj.responseText;59
}60
ajaxobj.send(); 61
}62
</script>63
</head>64

65
<body>66
67
<textarea name="result" id="result" cols="45" rows="5" style="width:200px; height:50px; border:#F00 1px double"></textarea>68
<!-- <div id="result"></div>-->69
<p>70
<input type="button" name="button" id="button" value="按钮" onclick="test();" />71
</p>72
</body>73
</html>74

处理页面ajax_use.php
<?php
echo date('y-m-d H:i:s');
?>
echo date('y-m-d H:i:s');
?>
P.S. 非常感谢提供帮助和代码的同事 王海


