ajax一个登陆的例子

<%@ 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*$/,"")或(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="登录" οnclick="loginzp()"/><!-- 此处按钮必须为为button,否则会造成表单提交,界面刷新,不是Ajax效果 -->
    </form>
     <div id="resultDiv"></div>
  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端代码: ``` <form> <label for="username">用户名:</label> <input type="text" id="username" name="username"><br><br> <label for="password">密码:</label> <input type="password" id="password" name="password"><br><br> <button type="button" onclick="login()">登录</button> </form> <script> function login() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; var xhr = new XMLHttpRequest(); xhr.open("POST", "/login"); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { alert(xhr.responseText); } }; xhr.send(JSON.stringify({username: username, password: password})); } </script> ``` 后端代码(使用Node.js和Express框架): ``` const express = require("express"); const bodyParser = require("body-parser"); const app = express(); app.use(bodyParser.json()); app.post("/login", (req, res) => { const username = req.body.username; const password = req.body.password; // 在这里进行登陆验证 if (username === "admin" && password === "password") { res.send("登录成功"); } else { res.status(401).send("用户名或密码错误"); } }); app.listen(3000, () => { console.log("服务器已启动"); }); ``` 在这个例子中,当用户点击“登录”按钮时,前端会发送一个包含用户名和密码的JSON数据到后端的`/login`路由。后端会进行登陆验证,如果用户名和密码正确,会返回一个成功的响应,否则会返回一个HTTP 401错误。前端会在收到响应后弹出一个提示框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值