var webpage = "ReadApartment.aspx?qu=" + svalue.toString();
//alert(webpage);
var result = "";
try
{
var xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
}
catch (e)
{
try
{
var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
var xmlHttp=false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
result = xmlHttp.responseText;
}
xmlHttp.open("GET",webpage,false);
xmlHttp.send(null);在ie,chrome上测试都通过,就是在ff上result得不到数据.....为空..为什么啊?恳请大大解答
你可以随便找一个库,很容易解决这个问题,如jquery
在ff3.5测下来代码可以执行, 最好加上if(xmlHttp.status==200){...}
在ajax的XMLHttpRequest.onreadystatechange方法的差异:在FF中当状态为1(即XMLHttpRequest已经调用open但还没有调用send时),FF则会继续执行onreadystatechange后面的代码,到执行完后面的代码后,在执行onreadystatechange在状态2,3,4的代码,而IE会等待状态2的到了,执行完onreadystatechange中状态2,3,4的代码后,继续执行后面的代码,这样问题就出现了,经常我们在onreadystatechange的代码要处理从服务器上获得的数据(这个数据只有在onreadystatechange的状态为4时,才可以得到),所以这在IE中不存在问题,因为它会等待onreadystatechange状态4到来以后,在执行onreadystatechange后面的数据,但是由于FF不会等到onreadystatechange状态4到来后在执行onreadystatechange后面的代码,所以后面的代码就不能处理从服务器上获得的数据
原文地址(http://hi.baidu.com/traindiy/blog/item/a1ad05b3d96190a0d8335a2d.html/cmtid/d8ed9aefbc6d25e4cf1b3ed7)按照上面说的,吧代码改成这样就可以了:
[code]function xmlHandle()
{
if(xmlHttp.readyState<4)
return;
else if(xmlHttp.readyState==4&&xmlHttp.status==200)
result = xmlHttp.responseText;
} xmlHttp.onreadystatechange=xmlHandle;
xmlHttp.open("GET",webpage,false);
xmlHttp.send(null);
if("\v"=="v")//判断是否为ie
xmlHttp.onreadystatechange=xmlHandle;
else
xmlHttp.onreadystatechange=xmlHandle();//ff下的onreadystatechange加括号.[/code]在修改当中发现..ff,ie,chrome对onreadystatechange的处理都是不一样的......
{
if(xmlHttp.readyState<4)
return;
else if(xmlHttp.readyState==4&&xmlHttp.status==200)
result = xmlHttp.responseText;
}
xmlHttp.onreadystatechange=xmlHandle;
xmlHttp.open("GET",webpage,false);
xmlHttp.send(null);
if("\v"=="v")//判断是否为ie
xmlHttp.onreadystatechange=xmlHandle;
else
xmlHttp.onreadystatechange=xmlHandle();//ff下的onreadystatechange加括号.