客户端与服务器数据交换XMLHttpRequest对象在各个浏览器的兼容

进行客户端与服务器数据交换时,XMLHttpRequest对象的定义与实现,简单例子如下:

function ajax_function(var1,var2){
  var xmlHttp = null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    try {
      // Internet Explorer
      xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch (e) {
        alert('Your browser does not support AJAX!');
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      var someDiv = document.getElementById('someDiv');
      someDiv.innerHTML=xmlHttp.responseText;
    }
  }
  xmlHttp.open('GET','www/index.php?param1='+var1+'¶m2='+var2,true);
  xmlHttp.send(null);
}

========================================================================================

xmlHttp.onreadystatechange=function() { 
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
    var someDiv = document.getElementById('someDiv');
    someDiv.innerHTML=xmlHttp.responseText;
  }
}
xmlHttp.open('GET','www/index.php?param1='+var1+'¶m2='+var2,true);
xmlHttp.send(null);






posted @ 2013-04-14 00:24  Iven_雨之恋  阅读(374)  评论(0编辑  收藏  举报