使用moogose连接MongoDB 并用ajax获取数据
1.mongoose官网的简介
Let's>
mongoose是对mongodb操作方式的一个简化
2.使用mongoose保存数据
var mongoose = require('mongoose')
mongoose.connect(
'mongodb://123.206.112.122:27017/blog')
var db = mongoose.connection;
db.on(
'error', console.error.bind(console, 'connection error:'));
db.once(
'open', function() { console.log(
'hehe')var essaySchema = mongoose.Schema({ id:Number,
title:String,
content:String
})
essaySchema.methods.log
= function(){ console.log(
this.title) }
var Essay = mongoose.model('Essay',essaySchema)
var essay1 = new Essay({id:1,title:'essay>
essay1.save(
function(err,essay1){if(err) return console.error(err); essay1.log()
})
});
3.ajax连接获取数据
$.ajax({ url:
'http://127.0.0.1:1724/', type:
'GET', dataType:
'json', timeout:
1000, cache:
false, success:
function(data){ console.log(data)
}
})
问题:chrome log报错 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
分析:跨域问题。
解决:chrome安装Allow-Control-Allow-Origin插件
得到json数据
页:
[1]