简单Ajax投票,FF,Safari中运行正常,而IE却不正常

来源:百度知道 编辑:UC知道 时间:2024/05/27 13:46:55
//Get Radio Id
var AllVotingCount=0;
var RadioSelectId=0;
function GetRadioId(idNumber){RadioSelectId = idNumber;}
//新建xmlHttp
var xmlHttp;
function CreatxmlHttp(){
try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){}
try {xmlHttp = new XMLHttpRequest();} catch(e){}
}

function AddVoting() {
AllVotingCount+= 1;
CreatxmlHttp()
xmlHttp.open("GET", "AddVoting.ashx?id="+RadioSelectId, false);
xmlHttp.onreadystatechange = AddVotingChange;
xmlHttp.send(null);
xmlHttp.abort();
}

function AddVotingChange(){
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status==200)
{
document.getElementById("ThanksText").innerHTML = "感谢您的第" + AllVotingCount + "次参与,返回成功!";
}}
}
使用上述方法创建的xmlHttp对象,在FF和Safari中可以投任意次,而在IE中却只能投票一次(但是重启IE后又可以投一次)。

这可以和 IE 的缓存有关

xmlHttp.open("GET", "AddVoting.ashx?id="+RadioSelectId, false);
的 url 段加一个无意义的随机数参数试试
xmlHttp.open("GET", "AddVoting.ashx?r=" + Math.random()+ "&id="+RadioSelectId, false);

IE要分版本,CreatxmlHttp函数

function CreatxmlHttp()
{
if(document.uniqueID) //IE
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else //Mozilla
{
xmlHttp = new XMLHttpRequest();
}
}