Ajax崩溃了IE 7

时间:2012-05-12 04:06:51

标签: ajax crash internet-explorer-7

这是我的ajax代码

function sendAjax(send_data,id)
{
    var ajaxobj;

    alert("After this alert problem occurs!");

    if (window.XMLHttpRequest) ajaxobj = new XMLHttpRequest();
    else ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");

    ajaxobj.onreadystatechange=function()
    {
      if(ajaxobj.readyState==4) 
      {
          if(ajaxobj.responseText.match("confirmPage") != null) document.getElementById(id).innerHTML = ajaxobj.responseText;
          else  
          {
            if(id == "FreshContent") 
            document.getElementById(id).innerHTML = "<a id=\"refreshpage\" onClick=\"siteSelection('select')\">Failed.Click here to Reload!</a>";
            else
            document.getElementById(id).innerHTML = "<a id=\"refreshpage\" onClick=\"sendAjax(0,'latest_gossip_marquee');\">Failed.Click here to Reload!</a>";
          }

          }
      else document.getElementById(id).innerHTML="Loading...."; 
    }

    if(id == "FreshContent") ajaxobj.open("GET","sitexyz.php?"+send_data,true);
    else ajaxobj.open("GET","html/xyz.html",true); 
    ajaxobj.send();     
}

这里FreshContent是一个div标签id.it适用于opera&amp; firefox但它在我的IE7中崩溃了。 为了查看服务器返回的页面是否有效,代码检查返回的页面是否包含confirmPage字符串。

1 个答案:

答案 0 :(得分:1)

尝试使用此功能 - 它比你正在使用的little more robust

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}