原生ajax示例XMLHttpRequest

来源:互联网 发布:子网网络号怎么算 编辑:程序博客网 时间:2024/06/05 09:46
   function  login(){    var username=document.getElementById("username").value;  var password=document.getElementById("password").value;  var proxyactivityId=document.getElementById("proxyactivityId").value;var proxyId=document.getElementById("proxyId").value;var postStr="username="+username+"&password="+password+"&lat="+latitude+"&lng="+longitude+"&proxyactivityId="+proxyactivityId+"&proxyId="+proxyId+"";var xmlhttp;if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function(){  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {  var code=xmlhttp.responseText;  if(code==1){  alert("消息:登录账号或者密码不能为空!");  }  if(code==2){  alert("消息:用户还未注册!请注册");  }  if(code==3){  alert("消息:登录密码错误!");  }  if(code==0){  document.getElementById("lat").value=latitude;  document.getElementById("lng").value=longitude;  var commit=document.getElementById("loginForm");  commit.submit();  }    }  };xmlhttp.open("POST","/webclient/login",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send(postStr);}