1

这是我的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 字符串。

4

1 回答 1

1

Try this function out - it's a little more robust than what you're using.

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;
}
于 2012-05-12T04:22:43.703 回答