0

我的firebase db上有一个超过11个病人的名单,我想做的是加载前5个病人。但我的问题是,当我单击下一个按钮时,它会重新加载页面,这是我的脚本。但是当我放在/:next我的端点上时,脚本不起作用。但是当我删除它时,它会加载前 5 个数据,但是当我单击下一个按钮/上一个按钮时,它只是重新加载页面。

router.get('/host/:next', function (req, res) {
    var ref = database.ref('patients').limitToFirst(5);

    var quickReplies = {
        messages: [
            {
                text:  "select now..",
                quick_replies: []
            }
        ]
    };

    var backButton = {
        "set_attributes":{
            "next":-1
        },
        "title":"\u23ea",
        "next":"0",
        "block_names":["list"]
    };

    var nextButton = {
        "set_attributes":{
        "next":1
        },
            "title":"\u23e9",
            "next":"0",
            "block_names":["list"]
    };

    ref.once('value', function (snapshot) {
        snapshot.forEach(function (childSnapshot) {
            var childKey = childSnapshot.key;
            var childData = childSnapshot.val();
            var setAttr = {
                set_attributes: {
                    title: childData.firstName,
                    next: 0
                },
                title: childData.firstName,
                next: 0,
                block_names: ['na']
            };

            quickReplies.messages[0].quick_replies.push(setAttr);
        });
        quickReplies.messages[0].quick_replies.unshift(backButton);
        quickReplies.messages[0].quick_replies.push(nextButton);
        res.send(quickReplies);
    });
}); 

这是我的 json 获取请求..localhost:8100/api/users/host?next={{next}}

下一个的默认值为 0..

4

1 回答 1

0

为了防止这种行为,您需要添加event.preventDefault()按钮单击事件。就像机 JS 或jQuery一样。
第二点,要使您的请求动态更改数据,您需要json从服务器发送并在前面解析它。
或者您可以将渲染视图直接发送到客户端,然后使用此视图更改您的 DOM。
例如像这样使用 jQuery load

$('body').load('<your api url which returns view');

或者,您可以使用$.get()请求对数据进行更多操作。像这样:

$.get('your api url which returns view or json', function(response){
  if(response.status === 'ok'){
    // append view or parse json
  } else {
    // some error with your data
  }
}).fail(function(){ //if server responds with 500 status });
于 2017-12-22T10:58:32.953 回答