Javascript 带模板循环的AJAX?
在本地运行基本AJAX web应用程序的情况下,表单通过Express/Node.js将数据发布到MongoDB,按钮onClick在div框中呈现Mongo文档时做出响应 使用模板时,单击按钮只会返回一个右大括号以显示在html div框中。 } …其中ajax在index.html中发布包装器数据:Javascript 带模板循环的AJAX?,javascript,ajax,node.js,mongodb,express,Javascript,Ajax,Node.js,Mongodb,Express,在本地运行基本AJAX web应用程序的情况下,表单通过Express/Node.js将数据发布到MongoDB,按钮onClick在div框中呈现Mongo文档时做出响应 使用模板时,单击按钮只会返回一个右大括号以显示在html div框中。 } …其中ajax在index.html中发布包装器数据: <!-- For the Returned Fields --> <div id="theResponse"> </div><!-- /.theRes
<!-- For the Returned Fields -->
<div id="theResponse">
</div><!-- /.theResponse -->
console.log(返回值);正在列出mongoDB文档:
{"keyName":"Here's a Value!"}, {"keyName":"Here's another Value!"}
…来自app.js
function getAllDOCs(res) {
db.collection('dbCollectionName').find({}, {"_id":0}).toArray(function (err, docs) {
console.log("Got the DOCs: " + docs);
var returnValue = "";
for (var i = 0; i < docs.length; i++)
{
if (returnValue == "")
{
returnValue = returnValue + JSON.stringify(docs[i]);
}
else
{
returnValue = returnValue + ", " + JSON.stringify(docs[i]);
}
console.log(docs[i]);
}
console.log(returnValue);
res.render('wrapper', { allDOCs: returnValue });
});
}
使用JSONP和JSON有一个很大的区别,第一个是在DOM中插入一个脚本标记,JSON封装在一个函数中,看起来不像你在服务器上输出的那样。非常感谢你的见解。已更新app.js代码段以包含更多详细信息。。有什么建议吗?
function getAllDOCs(res) {
db.collection('dbCollectionName').find({}, {"_id":0}).toArray(function (err, docs) {
console.log("Got the DOCs: " + docs);
var returnValue = "";
for (var i = 0; i < docs.length; i++)
{
if (returnValue == "")
{
returnValue = returnValue + JSON.stringify(docs[i]);
}
else
{
returnValue = returnValue + ", " + JSON.stringify(docs[i]);
}
console.log(docs[i]);
}
console.log(returnValue);
res.render('wrapper', { allDOCs: returnValue });
});
}
$.ajax({
dataType: 'jsonp',
jsonpCallback: '_wrapperGet',
data: $('#theForm').serialize(),
type: 'POST',
url: "http://localhost:9090/insert",
success: handleINSERTbuttonResponse,
});
function handleFINDbuttonResponse(data)
{
// parse the json string
var jsonObject = JSON.parse(data);
$('#theResponse').append( jsonObject.getting );
}