ajax的基础使用

ajax的基础应用

/**

     这是一段 javaScript ajax的一段基础应用实例

**/

//这段代码是 创建一个XMLHttpRequest对象

function initAjax(){//初始化,指定处理函数,发送请求的函数
 http_request=false;
 //开始初始化XMLHttpRequest对象
 if(window.XMLHttpRequest){//Mozilla浏览器
  http_request=new XMLHttpRequest();
  if(http_request.overrideMimeType){//设置MIME类别
   http_request.overrideMimeType("text/xml");
  }
 }
 else if(window.ActiveXObject){//IE浏览器
  try{
   http_request=new ActiveXObject("Msxml2.XMLHttp");
  }catch(e){
   try{
    http_request=new ActiveXobject("Microsoft.XMLHttp");
   }catch(e){}
  }
 }
 if(!http_request){//异常,创建对象实例失败
  window.alert("创建XMLHttp对象失败!");
  return false;
 }
 return http_request;

}

 

//---------------------------------------------------------------------------

 

function rightinfo()
{
 var url;

var qqAddress=./ajax/rr.jsp;//这是一个请求的路径可以是一个页面,也可以是一个 servlet
 var ajax = initAjax();
 url = encodeURI('qqAddress?r='+ Math.random());
 ajax.open('post',url,true);
 ajax.send(null);
 ajax.onreadystatechange = function()
 {
  
  if(ajax.readyState == 4)
  {
   
   document.getElementById('friendList').innerHTML = ajax.responseText;

          //这个地方写自己的方法

          //ajax.responseText   这是异步请求的 得到的一个结果对象

          alert("执行自己的代码");
   }
 }
}