手写ajax

function createAjaxObj(){
 var httprequest=false
 if (window.XMLHttpRequest){ // if Mozilla, Safari etc
  httprequest=new XMLHttpRequest()
  if (httprequest.overrideMimeType)
   httprequest.overrideMimeType('text/xml');
 }
 else if (window.ActiveXObject){ // if IE
  try
  {
   httprequest=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
   try
   {
    httprequest=new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (e){}
  }
 }
 return httprequest;
}

 

 

 

 

 

postStr+="&lan="+document.getElementById("lanVersion").value;

// alert(postStr);
 //实例化Ajax
 var ajax = createAjaxObj(); 
 //通过Post方式打开连接
 ajax.open("POST", url, true);
 //定义传输的文件HTTP头信息,这一行不能少
 ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
 //发送POST数据
 ajax.send(postStr);
 ajax.onreadystatechange = function() {
  if (ajax.readyState == 4 && ajax.status == 200) {
//        document.getElementById("content").value=""
//        document.getElementById("bntSendProEmail").disabled=false;
        var content=ajax.responseText;
        alert(content);
  }
  else if(ajax.readyState != 1 && ajax.readyState != 2 && ajax.readyState != 3&& ajax.readyState != 0)
  {
//   document.getElementById("bntSendProEmail").disabled=true;
   alert("评论发表失败,评论内容含有非法信息!");
  }
 }

 

 

你可能感兴趣的:(Ajax)