subform.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="lib/jquery.min.js"></script>
</head>
<body>
<form>
姓名:<input type="text" name="name">
年龄:<input type="text" name="age">
密码:<input type="text" name="pwd">
<input type="button" id="btn" value="提交" >
</form>
<input type="button" id="getbtn" value="获取数据">
<p></p>
<script>
$("#btn").click(function(){
$.ajax({
url:"/insert",
type:"post",
data:$("form").serialize(),
success:function(data){
if(data==1){
alert("保存成功");
}else{
alert("保存失败");
}
}
});
});
$("#getbtn").click(function(){
$.ajax({
url:"/getworker",
type:"get",
data:{"age":12},
success:function(data){
// alert(data.toString());
$.each(data,function(i,v){
$("body p").append(v._id+" "+v.name+" "+v.age+
"|<input type='button' id="+v._id+" value='删除'>" +
"<a target='_blank' href='/editworker/"+v._id+"'>修改</a><br>");
});
addclick();
}
});
});
function addclick(){
$("input[value='删除']").click(function(){
alert("bbb");
$.ajax({
url:"/delworker",
type:"get",
data:{id:$(this).attr("id")},
success:function(data){
if(data=="1"){
alert("删除成功");
}else{
alert("删除失败");
}
},error:function(msg){
console.log(msg);
}
});
})
}
</script>
</body>
</html>
server.js
因为需要按_id操作(mongodb.ObjectId(req.query.id))
所以引入mongodb包
var express = require("express");
var mongodb = require("mongodb");
var bodyParser = require("body-parser");
var db = require("./module/db.js");
var app = express();
app.use(express.static("./public"));
/**
* bodyParser.json()和bodyParser.urlencoded()区别:
* 一个接受form请求,一个接受json请求,解析的规则不一样
*/
app.use(bodyParser.urlencoded({extended:false}));
app.post("/insert",function(req,res){
console.log(req.body);
db.insert("worker",{
name:req.body.name,
age:req.body.age,
pwd:req.body.pwd
},function(err,results){
if(err){
res.json("-1");
}else{
res.json("1");
}
});
});
app.get("/getworker",function(req,res){
db.find("worker",{"age":req.query.age},function(err,results){
if(err){
res.json("-1");
}else{
res.json(results);
}
});
});
app.get("/delworker",function(req,res){
db.delete("worker",{"_id":mongodb.ObjectId(req.query.id)},function(err,results){
if(err){
res.json("-1")
}else{
res.json("1")
}
})
});
app.get("/edit/worker/:id",function(req,res){
// req.send(req.params['id']);
})
app.listen(80);
db.js
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
function _connect(callback){
var url = "mongodb://localhost:27017/company";
MongoClient.connect(url,function(err,db){
callback(err,db);
db.close();
})
}
exports.find = function(collectionName,json,callback){
_connect(function(err,db){
db.collection(collectionName).find(json).toArray(function(err,results){
callback(err,results);
});
})
}
exports.insert = function(collectionName,json,callback){
_connect(function(err,db){
db.collection(collectionName).insertOne(json,function(err,results){
callback(err,results);
})
});
}
exports.delete = function(collectionName,json,callback){
_connect(function(err,db){
db.collection(collectionName).deleteOne(json,function(err,results){
callback(err,results);
})
});
}