weixin_33701564 2013-12-24 18:40 采纳率: 0%
浏览 32

具有模板循环的AJAX?

With a basic AJAX web app running locally, a Form POSTs data through Express/Node.js to MongoDB, and a button onClick responds in a rendering of the Mongo document(s) in a div-box.

Using Swig for templating, onClick of the button is only returning a right-brace to appear in the html div-box.

}

..where the ajax posts the wrapper data in index.html:

<!-- For the Returned Fields -->
<div id="theResponse">

</div><!-- /.theResponse -->

How can the jsonpCallback or Templating-Loop be edited to push all the MongoDB docs?

wrapper.html:

{% for go in allDOCs %}
_wrapperGet('{"getting":"{{ keyName }}"}')
{% endfor %}

console.log(returnValue); is listing the mongoDB documents:

{"keyName":"Here's a Value!"}, {"keyName":"Here's another Value!"}

..from 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 });

    });
}

index.html AJAX json parsing:

$.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 );
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题