加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax一个登陆的例子

发布时间:2020-12-16 01:03:58 所属栏目:百科 来源:网络整理
导读:%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% %@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用户登陆</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.2.js"></script> <script type="text/javascript" charset="utf-8"> function loginzp(){ var xmlHttp=false; var username=document.zpform.username.value; var password=document.zpform.password.value; var url="/testajax/log/log1?username="+username+"&password="+password; if(window.XMLHttpRequest){//Mozilla等浏览器 xmlHttp = new XMLHttpRequest(); } else if(window.ActiveXObject){//IE浏览器 try{ xmlHttp = new ActiveXObject("Msxm12.XMLHTTP"); }catch(e){ try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ window.alert("该浏览器不支持Ajax"); } } } xmlHttp.open("post",url,true); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4&&xmlHttp.status == 200){ //xmlHttp.readyState==4请求是否完成,xmlHttp.status == 200请求是否成功 //resultDiv.innerHTML = xmlHttp.responseText; //得到响应值 var str = xmlHttp.responseText.replace(/^s*/,"").replace(/s*$/,""); //alert("""+xmlHttp.responseText+"""); alert(str);//判断是false或是true //alert(typeof('success')); alert(str=="success");//输出响应信息 if(str=="success") //加上.responseText.replace(/^s*/,"")或(replace(/^s*|s*$/g,''))替换前后空格,否则也可用数字判断,而用中文或字母不行(在页面loginSuccess中设的值是success,longinFail中设置的值为0) { window.location.href="/testajax/log/log2";//又通过一次struts跳转到另外一个页面 } //alert(str); //alert(str==0);//输出响应信息 if(str==0) { document.getElementById("s1").innerHTML = "登录名不存在!";//输出错误信息 } }else{ document.getElementById("resultDiv").innerHTML = "正在登录,请稍后......"; } } //alert(url);//F12调试模式查看输出 xmlHttp.send(); } </script> </head> <body>欢迎进入学生管理系统<hr/> <form name="zpform"> 用户名:<input type="text" name="username"><span id="s1"></span><br> 密码:<input type="password" name="password"><span id="s2"></span><br> <input type="button" value="登录" onclick="loginzp()"/><!-- 此处按钮必须为为button,否则会造成表单提交,界面刷新,不是Ajax效果 --> </form> <div id="resultDiv"></div> </body> </html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读