html :
<div class="login">
<form name="cForm">
<ul>
<li>
<label>Username:</label>
<input type="text" name="user_name"/>
</li>
<li>
<label>Password: </label>
<input type="password" name="user_pass"/>
</li>
<input type="submit" value="Login" />
<input type="button" value="Forget" />
</ul>
</form>
</div>
<form name="cForm">
<ul>
<li>
<label>Username:</label>
<input type="text" name="user_name"/>
</li>
<li>
<label>Password: </label>
<input type="password" name="user_pass"/>
</li>
<input type="submit" value="Login" />
<input type="button" value="Forget" />
</ul>
</form>
</div>
js:
// JavaScript Document
$(document).ready(function(){
//登录窗体显示
$('#login').click(function(){
$('.login').css("visibility","visible").fadeToggle("slow");
});
$(document).ready(function(){
//登录窗体显示
$('#login').click(function(){
$('.login').css("visibility","visible").fadeToggle("slow");
});
/*AJAX文本*/
//登录
$(".login input[type='submit']").click(function(){
var http ;
if(window.XMLHttpRequest){
http = new XMLHttpRequest() ; //IE7+,firefox,Chorme...
}else{
http = new ActiveXObject('Microsoft XMLHTTP') ; //IE5,6
}
//http.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
var name = $(".login input[name='user_name']").val() ;
var pass = $(".login input[name='user_pass']").val() ;
var user={
username:name,
userpass:pass
};
alert("提交登录信息" +user.username +":"+user.userpass) ;
http.open("POST","/login_user/?user_name="+user.username +"&user_pass="+user.userpass,true) ;
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
http.send(null) ;
alert("登录信息提交成功") ;
http.onreadystatechange=function(){
alert("成功!") ;
if(http.status === 200){
//$(".register").css("visibility","hidden") ;
}
} ;
//if(http.responseText === 'ok'){
// // $(".register").css("visibility","hidden") ;
// //}
//
//
}) ;
});
NodeJS:(mysqlConnection是我连接的数据库)
var express = require('express') ;
//登录请求
app.post('/login_user',function(req,res){
console.log(url.parse(req.url).pathname +"::" +JSON.stringify(req.query)) ;
var user = {
my_name: req.query.user_name,
my_pass: req.query.user_pass
};
console.log("login请求: " +"用户---" +user.my_name +"+"+user.my_pass) ;
mysqlConnection.query("SELECT * FROM " +MY_TABLE
+" WHERE name='"
+user.my_name
+"' AND password='" +user.my_pass +"'",
function(err,results,fields){
if(err){
throw err ;
console.log("出现错误") ;
}
if(results!= 0){
console.log("查找用户成功!" +JSON.stringify(results)) ;
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://127.0.0.1:8081"});
//res.sendFile("E:/HtmlProject/node_modules/public/index-user.html") ;
console.log("登录成功!") ;
res.send('ok') ;
res.status(200).json(
{'user': user.my_name},
{'Content-Type':'text/html'},
{"Access-Control-Allow-Origin":"http://127.0.0.1:8081/login_user?user_name="+user.my_name +
"&user_pass="+user.my_pass}
).end() ;
}else{
console.log("查找用户失败!" +JSON.stringify(results)) ;
}
}
);
app.post('/login_user',function(req,res){
console.log(url.parse(req.url).pathname +"::" +JSON.stringify(req.query)) ;
var user = {
my_name: req.query.user_name,
my_pass: req.query.user_pass
};
console.log("login请求: " +"用户---" +user.my_name +"+"+user.my_pass) ;
mysqlConnection.query("SELECT * FROM " +MY_TABLE
+" WHERE name='"
+user.my_name
+"' AND password='" +user.my_pass +"'",
function(err,results,fields){
if(err){
throw err ;
console.log("出现错误") ;
}
if(results!= 0){
console.log("查找用户成功!" +JSON.stringify(results)) ;
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://127.0.0.1:8081"});
//res.sendFile("E:/HtmlProject/node_modules/public/index-user.html") ;
console.log("登录成功!") ;
res.send('ok') ;
res.status(200).json(
{'user': user.my_name},
{'Content-Type':'text/html'},
{"Access-Control-Allow-Origin":"http://127.0.0.1:8081/login_user?user_name="+user.my_name +
"&user_pass="+user.my_pass}
).end() ;
}else{
console.log("查找用户失败!" +JSON.stringify(results)) ;
}
}
);
});
我测试是能够成功的。希望对大家有帮助。毕竟我也是先手,研究一段时间得出来得