使用cookie即可。
<!DOCTYPE HTML><html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
</style>
<script type="text/javascript">
function readCookie (name)
{
var cookieValue = ""
var search = name + "="
if (document.cookie.length > 0)
{
offset = document.cookie.indexOf (search)
if (offset != -1)
{
offset += search.length
end = document.cookie.indexOf ("", offset)
if (end == -1)
end = document.cookie.length
cookieValue = unescape (document.cookie.substring (offset, end))
}
}
return cookieValue
}
function writeCookie (name, value, hours)
{
var expire = ""
if (hours != null)
{
expire = new Date ((new Date ()).getTime () + hours * 3600000)
expire = " expires=" + expire.toGMTString ()
}
document.cookie = name + "=" + escape (value) + expire
}
writeCookie ("myCookie", "my name", 24)
alert (readCookie ("myCookie"))
</script>
</head>
<body>
</body>
</html>
nodejs数据存mongodb:数据库创建名 users 集合并插入条用户信息前没 users 集合mongodb 直接创建
>db.users.insert( { "userId":1, "name":"tom", "email":"tom@nodejs.org" })
查找信息使用 find 或者 findOne区别于 findOne 返结
db.users.findOne( {"userId": 1})
返结:
{
"_id" : ObjectId("5413be6e9e1c9f9c4386756d"),
"userId" : 1,
"name" : "tom",
"email" : "tom@nodejs.org"
}
驱程序
编辑 package.json, 添加于 mongodb 引用
{
"name": "express-api",
"version": "0.0.1",
"dependencies": {
"express": "2.5.9",
"ejs": "0.4.2",
"mongodb": "1.4.1"
}
}
重新 npm install 安装 mongodb 驱
使用 MongoDB 数据库
修改代码首先 require mongodb 模块连接 mongodb 数据库
var mongo = require("mongodb")
var express = require("express")
var app = express.createServer()
app.set("view engine", "ejs")
app.set("views", __dirname + "/views")
app.set("view options", { layout: false })
app.get("/", function (request, response) {
response.render("index")
})
app.get("/user/:id", function (request, response) {
var id = request.params.id
console.log(id)
app.users.findOne({ "userId": +id }, function (error, doc) {
if (error) return next(error)
response.json(doc)
})
})
// connect mongodb
var server = new mongo.Server("127.0.0.1", 27017)
var db = new mongo.Db("members", server, {safe:true }).open(function (error, client) {
if (error) throw error
console.log("\033[96m + \033[39m connected to mongodb")
app.users = new mongo.Collection(client, "users")
client.ensureIndex("users", "userId", function (error) {
if (error) throw error
console.log("\033[96m + \033[39m ensured index.")
console.log("Web Server listening ......")
app.listen(3000)
})
})
注意现数据库查找用户id 前面 + 用表单字符串类型数据转换需要数字类型
app.users.findOne({ "userId": +id }, function (error, doc) {
if (error) return next(error)
response.json(doc)
})
$.ajax({type: 'post',
url: getContentPath() + '/zmanaly/readTotal',
data: {"type":type}, //此处输入传入后台的值,“”里面则是后台用哪个接收
dataType:'text',
async:false,
success: function(data){
}
}
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)