我的第一个ajax脚本

   代码如下

//创建XMLHttpRequest对象

var xmlHttp=null;

function creatXMLHttp(){

       try{

          xmlHttp = new XMLHttpRequest();           

    }catche(e){

       var msxmlhttp = new Array("Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0",

                                            "Microsoft.XMLHTTP") 

              for(var i =0;i < msxmlhttp.length();i++){

              try{

                xmlHttp = new ActiveXObject(msxmlhttp.[i]) ;

                if(xmlHttp!=null){break;} 

               }catch(e){}

         }

      }

              if(xmlHttp==null){alert("浏览器不支持Ajax");}

}

    function sendRequest(url,params,method,handler){

                       

                          xmlHttp.onreadystatechange="handler";//指定处理函数

                         if(mehod=="GET"){

                         xmlHttp.open(method,url+'?'+params ,true)

                          xmlHttp.send(null);                                            

  }  

                         if(method="POST"){

                               xmlHttp.open(mehod,url,true) ;

                               xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded")

                               xmlHttp.send(params);     

    }

}

//

function formCheck(){
var url ="logcheck";
var userName=document.getElementById("luserName").value;
var psd=document.getElementById("lpsd").value;
var yanzheng=document.getElementById("lyanzheng").value;
var params="userName="+userName+"&psd="+psd+"&yanzheng="+yanzheng;
sendRequest(url,params,'POST',showResult);
}
function showResult(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
var info =xmlHttp.responseText;
if(!(info==1)){
alert(info);
}
else{
window.location.href="/studentlogin/admin/home.jsp";
}
}

原文地址:https://www.cnblogs.com/code-changeworld/p/4128129.html