Skip to main content
 首页 » 编程设计

ajax之Ajax使IE 7崩溃

2025年05月04日130bluestorm

这是我的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。它可以在Opera&firefox中工作,但在我的IE7中会崩溃。
要查看服务器返回的页面是否有效,代码将检查返回的页面中是否包含ConfirmPage字符串。

请您参考如下方法:

试试这个功能-比您正在使用的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; 
}