﻿/**************************************************
*本函数是通用的调用ajax的函数框架
*对服务器状态的响应由参数callback决定
*url:           将请求发送到的地址
*method:        请求的类型,get或者post
*content:       要传送给服务器的内容
*responseType:  返回类型，responseText或者responseXML
*callback:      执行回调的函数名
*requestType:   请求类型，ture为异步，false为同步
***************************************************/
function doSend(url,method,content,responseType,callback,requestType){
   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{//IE 5.0以下版本
            http_request=new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(e){}
      }
   }
   if(!http_request){//创建XMLHttpRequest对象失败
      window.alert("不能创建XMLHttpRequest对象，请检查浏览器设置!");
      return false;
   }
   
   //确定获取请求的方式
   if(responseType.toLowerCase()=="text" || responseType.toLowerCase()=="xml"){
      http_request.onreadystatechange=callback;
   }
   else{
      window.alert("响应类别参数出错");
      return false;
   }
   
   //确定发送请求方式
   if(method.toLowerCase() == "get"){
      http_request.open(method,url,requestType);
   }
   else if(method.toLowerCase() == "post"){
      http_request.open(method,url,requestType);
      http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   }
   else{
      window.alert("Http请求类型出错");
      return false;
   }
   http_request.send(content);    
}

