【发布时间】:2014-04-28 03:27:33
【问题描述】:
所以我的 nodeJs 服务器代码中有这个方法,它需要向 mongoDB 插入一些东西,问题是即使它插入了什么(我在它之后的数据库中看到)它也不会进入成功调用。
我只想在我的客户中发出警报说“完成”
我正在通过 express 使用 nodejs。
这是我的服务器端:
exports.savePre = function(db) {
return function(req, res) {
// Get our form values. These rely on the "name" attributes
var json = req.body.jsonToSave;
var date = req.body.date;
var name = req.body.name;
var ArrayOfSlice = req.body.ArrayOfSlice;
// Set our collection
var collection = db.get('PresentationCollection');
// Submit to the DB
collection.insert({
"JsonToSave": json,
"Date": date,
"Name": name,
"ArrayOfPoints": ArrayOfSlice
}, function (err, inserted)
{
res.writeHead(200, { 'content-type': 'text/plain' });
res.end();
if (err)
{
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else
{
res.send("bla");
//res.writeHead(200, { 'content-type': 'text/plain' });
//res.end();
//res.setHeader("Access-Control-Allow-Origin", "*");
// If it worked, set the header so the address bar doesn't still say /test
//res.end();
// // And forward to success page
// res.redirect("userlist");
}
});
}
}
这是我在客户中的电话:
function savePre()
{
var exporter = new THREE.SceneExporter();
var sceneJInson = JSON.stringify(exporter.parse(scene));
var CameraPosition = camera.position;
//var CameraLookAt = lookAtPosition;
// Array Of Slice, each row is a slice - here we will add all the slices :)
var arrayOfSlice = [{ cameraPosition: CameraPosition }];
// { cameraPosition: CameraPosition, CameraLookAt: CameraLookAt },
// { cameraPosition: CameraPosition, CameraLookAt: CameraLookAt }];
// TODO : give the name where the user chose, this is only test, date is also a test
var parameters = { name: "eranNew", date: new Date(), jsonToSave: sceneJInson, ArrayOfSlice: JSON.stringify(arrayOfSlice) };
$.ajax({
url: '/savePre',
type: 'POST',
data: parameters,
success: function (result) {
alert("done");
},
error: function (result) {
alert("error");
},
dataType: 'json'
});
}
任何帮助都会得到回报。
【问题讨论】:
标签: jquery ajax node.js mongodb