Ajax post提交与status=0的解决方法
发布时间:2020-12-16 02:12:23 所属栏目:百科 来源:网络整理
导读:POST请求 //创建Ajax引擎function getXmlHttpObject() {var xmlHttp=null;try {// Firefox,Opera 8.0+,SafarixmlHttp=new XMLHttpRequest();//alert("ff");} catch (e) {// IEtry {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//alert("1");} catch (e) {/
POST请求 //创建Ajax引擎 function getXmlHttpObject() { var xmlHttp=null; try { // Firefox,Opera 8.0+,Safari xmlHttp=new XMLHttpRequest(); //alert("ff"); } catch (e) { // IE try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); //alert("1"); } catch (e) { // TODO: handle exception xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); //alert("2"); } } return xmlHttp; } function checkLogin() { var info=document.getElementById("re"); var name=document.getElementById("username"); var pwd=document.getElementById("password"); if(!name.value){ info.value="请输入学号"; info.style.visibility="visible"; //重新获得焦点 name.focus(); return false; } if(!pwd.value){ info.value="请输入密码"; info.style.visibility="visible"; //密码框获得焦点 pwd.focus(); return false; } //异步验证 var xmlHttp=getXmlHttpObject(); if(xmlHttp){ //alert("123"); var url="http://localhost:8080/Diandian/LoginCSerlvet"; var data="username="+name.value+"&password="+pwd.value; //第三个参数true表示使用异步机制,false表示不用异步机制 xmlHttp.open("POST",url,true); //必须加这个请求头 xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlHttp.send(data); xmlHttp.onreadystatechange=function(){ //alert(xmlHttp.readyState+" "+xmlHttp.status); if(xmlHttp.readyState==4&&xmlHttp.status){ var str=xmlHttp.responseText; //alert("12"); if(str=="0"){ info.value="学号与密码不正确"; info.style.visibility="visible"; //把密码清零获取焦点 pwd.value=""; pwd.focus(); return false; }else{ //alert("ok"); location.href=str; } } }; } }解决status=0的方法 把submit提交按钮改为button按钮 <input type="button" value="登录" class="sub_css" onclick="return checkLogin()"> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |