node代码
const express = require('express'); //引入模块
const ex_static = require('express-static'); //读取静态文件
var server = express(); //创建服务
server.listen(8080);//监听
//用户数据
var users = {
'aaa':'111',
'555':'444',
'aaaa':'55555'
};
server.get('/login',function(req,res){
var user = req.query['user'];
var pass = req.query['pass']; //express模块封装方法,得到user和pass
//检测用户名与密码是否匹配
if(users[user] == null){
res.send({ok:false,msg:'此用户不存在'});
}else{
if(users[user]!=pass){
res.send({ok:false,msg:'用户名或者密码错误'});
}else{
res.send({ok:true,msg:'登录成功'});
}
}
});
server.use(ex_static('./www'));
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
用户名:<input type="text" id="user"><br/>
密码:<input type="password" id="pass"><br/>
<input type="button" value="登录" id="login_btn">
<script src="ajax.js"></script>
<script>
window.onload = function(){
var user1 = document.getElementById('user');
var pass1 = document.getElementById('pass');
var login_btn1 = document.getElementById('login_btn');
login_btn1.onclick = function(){
ajax({
url:'/login',
data:{user:user1.value,pass:pass1.value},
success:function(str){
var json = eval('('+str+')');
if(json.ok){
alert('登录成功');
}else{
alert('登录失败:' + json.msg);
}
},
error:function(){
alert('通信错误');
}
});
};
};
</script>
</body>
</html>