日期:2014-05-18  浏览次数:20545 次

求解!初学ajax
为什么只在ie上好使,在火狐上不好使呢?
var   xmlhttp;
          function   createHTTP()
          {
              //根据不同的浏览器创建XMLHttpRequest
                if(window.ActiveXObject)
                {
                      xmlhttp=new   ActiveXObject( "Microsoft.XMLHTTP ");
                }
                else   if(window.XMLHttpRequest)   //ff,safari
                {
                      xmlhttp=new   XMLHttpRequest();
                }
          }
          //开始调用
          function   startHTTP()
          {
              //创建对象
                createHTTP();
                //状态变化与事件挂钩
                xmlhttp.onreadystatechange=StateDO;
                //获取XML文件的数据
                xmlhttp.open( "GET ", "XMLFile.xml ",true);
                //不带任何参数
                xmlhttp.send(null);
          }
          function   StateDO()
          {
          //判断是否是完成状态
              if(xmlhttp.ReadyState==4)
                  {
                  //判断是否执行成功
                      if(xmlhttp.Status==200)
                      {
                      //更新页面上的某元素
                            document.getElementById( "mytext ").innerText=xmlhttp.responsetext;
                      }
                  }
          }

------解决方案--------------------

javascript对大小写是敏感的。ie兼容性比较强,会帮你处理,ff就不行了。
xmlhttp.ReadyState应为:xmlhttp.readystate
xmlhttp.Status 应为;xmlhttp.status
xmlhttp.responsetext 应为xmlhttp.responseText

还有,
getElementById( "mytext ").innerText 只是ie的属性,通用的应该写:
getElementById( "mytext ").innerHTML


------解决方案--------------------
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP ");
}
else if(window.XMLHttpRequest) //ff,safari
{