//发送数据
function AjaxSend(url,name,value){
xmlhttp = createXMLHttpRequestOblect();
xmlhttp.open('get',url,false);
var body=name+"="+value;
var result="";
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
result = unescape(xmlhttp.responseText);
}else{
result=xmlhttp.status;
}
}
}
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.send(body);
return result;
}
我在IE中测试可以获取数据,就是result有结果,但是firefox中result的值依旧是“”,这个是为什么?

解决方案 »

  1.   

     if(window.ActiveXObject)
            {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if(window.XMLHttpRequest)
            {
                xmlhttp = new XMLHttpRequest();
            }
      

  2.   

    如楼上 xmlhttp对象IE和FF不同。。
      

  3.   

    做过兼容判断的:这个是创建对象的函数
    function createXMLHttpRequestOblect()
    {
        try
        {
          xmlhttp = new XMLHttpRequest();
        }
        catch(e)
        {
             var XMLHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                             "MSXML2.XMLHTTP.5.0",
                                             "MSXML2.XMLHTTP.4.0",
                                             "MSXML2.XMLHTTP.3.0",
                                             "MSXML2.XMLHTTP",
                                             "Microsoft.XMLHTTP");
            for (var i=0; i < XMLHttpVersions.length && !xmlhttp; i++ )
            {
                try
                {
                     xmlhttp = new ActiveXObject(XMLHttpVersions[i]);
                }
                catch (e) {}     
            }
         }
       if(!xmlhttp)
           alert("Error Creating the XMLHttpRequest Oblect.");
       else
           return xmlhttp;
    }
      

  4.   

    关键在这里
    if(xmlhttp.readyState==4){ 
      if(xmlhttp.status==200){ 
         result = unescape(xmlhttp.responseText); 
         //如果这里加alert(result)结果也是正确的,但是末尾返回的result就是"";
      }else{ 
         result=xmlhttp.status; 
      }

      

  5.   

    火狐下用   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     创建xmlhttp对象