遇到一个经典ajax错误

来源:百度知道 编辑:UC知道 时间:2024/07/07 00:42:15
首次提交能得到返回,以后就得不到返回值!
是得不到返回,服务器日志上显示有请求并执行
逻辑:
abc.jsp 通过 select的onchange事件以GET方式发送参数到服务器的action类,后带一个随机数。action得到参数查询数据库返回一组数据给页面。
现在就是第一次能得到返回值,后面就不能了,通过浏览器直接输入[action地址?参数名=参数值]能每次得到结果。
怎么回事?
var xmlhttp = null;

try {
xmlhttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlhttp = false;
}
}
}

if (!xmlhttp) alert("Error initializing XMLHttpRequest!");

function updateBig(firstIndex) {

var url="secondAction.action?firstIndexName="+firstIndex+"&random="+Math.random();
alert(url);
xmlhttp.onreadystatechange = updateSecondIndex;
xmlhttp.open("POST",url,true);
xmlhttp.send(null)

我以前也好像遇到过这类问题,怎么解决的忘了

把代码贴出来看下?

这是贴的什么啊。方法好像不全啊

random="+Math.random();
别人都是加时间戳,你用这个, 有思想。

你的XMLHttpRequest对象xmlhttp的初始化赋值有点问题,将代码改成:
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

并将请求的url改换成:
var url="secondAction.action?firstIndexName="+firstIndex+"×tampt=" + new Date().getTime();

就可以了

页面缓存的问题
head里加入
<META HTTP-EQUIV="pragma" CONTENT="no-cache">