ajax之post实践

服务器文件

//服务器端的文件
const express=require('express');
//psot
const bodyParser=require('body-parser');
const demoRouter=require('./routers/demo.js');
const ajaxRouter=require('./routers/ajax.js');
var server=express();
server.listen(8888);
//托管静态资源到public
server.use(express.static('./public'));

//使用路由器管理路由
//把用户路由器挂载到/user,访问形式/user/detail,所以HTML中要改
server.use(bodyParser.urlencoded({
	extended:false
}));
server.use('/demo',demoRouter);
server.use('/ajax',ajaxRouter);

路由器文件

const express=require("express");
const pool=require("../pool.js");
var router=express.Router();
router.get("/ajaxHappy",(req,res)=>{
	res.send("这个输入框必填");
});
router.get("/login",(req,res)=>{
	var reqObj=req.query;
	var uname=reqObj.uname;
	var upwd=reqObj.upwd;
	if(!uname){
		res.send({code:300,msg:"uname require"});
		return;
	}
	if(!upwd){
		res.send({code:300,msg:"upwd require"});
		return;
	}
	//res.send({code:200,msg:"success"});
	//查询数据库
	pool.query(`select * from reg where username=? and password=?`,[uname,upwd],(err,result)=>{
		if(err){throw err;}
		var i=result.length;
		if(i>0){
			res.send("登录成功");
		}else{
			res.send("用户名密码不正确");
		}
	});
});

//注册
router.post("/reg",(req,res)=>{
	var reqObj=req.body;
	var username=reqObj.username;
	var password=reqObj.password;
	var emile=reqObj.emile;
	var phone=reqObj.phone;
	if(!username){
		res.send("username required!");
		return;
	}
	if(!password){
		res.send("password required!");
		return;
	}
	if(!emile){
		res.send("emile required!");
		return;
	}
	if(!phone){
		res.send("phone required!");
		return;
	}
	pool.query(`insert into reg set ?`,[reqObj],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("注册成功");
		}else{
			res.send("注册失败");
		}
	})
});
router.get("/checkuname",(req,res)=>{
	var reqObj=req.query;
	var uname=reqObj.uname;
	var upwd=reqObj.upwd;
	if(!uname){
		res.send("用户名必填");
		return;
	}
	if(!upwd){
		res.send({code:300,msg:"upwd require"});
		return;
	}
	pool.query(`select * from reg where username=?`,[uname],(err,result)=>{
		if(err){throw err;}
		var i=result.length;
		if(i>0){
			res.send("用户存在");
		}else{
			res.send("用户不存在");
		}
	});
});


module.exports=router;

HTML文件

<!doctype html>
<html>
	<head>
	<title></title>
	<meta charset="utf-8">
	<meta http-equiv="Cache-Control" content="max-age=3600">
	</head>
	<body>
		<div>
		用户名:<input type="text" id="username" onblur="reg()">
		</div>
		<div>
		密码:<input type="password" id="password" onblur="reg()">
		</div>
		<div>
		邮箱:<input type="email" id="emile" onblur="reg()">
		</div>
		<div>
		电话:<input type="tel" id="phone" onblur="reg()">
		</div>
		<input type="button" value="提交" onclick="reg()">
		<div id="d1"></div>
		<script>
			function reg(){
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4&xhr.status==200){
						var result=xhr.responseText;
						console.log(result);
						d1.innerHTML=result;
					}
				}
				xhr.open('post','/ajax/reg',true);
				//修改消息头,由于默认请求时text/plain,无法传递URL中的符号
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				
				var formdata="username="+username.value+"&password="+password.value+"&emile="+emile.value+"&phone="+phone.value;
				console.log(formdata);
				xhr.send(formdata);
			}
		</script>	
	</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值