weixin_33726318 2019-12-05 21:29 采纳率: 0%
浏览 181

在Mongodb中使用Ajax

I've been looking for this for quite a while and I have no clue. I want to use Ajax to display information from a collection called 'reizen' in mondodb. I want to be able switch through all the elements from the collection and get all the information like the title etc. I have no idea how to get the information, I allready have a bit of code but I've been stuck for days, so I hope somebody can help me. If I only know how to get the title from each element from the collection 'reizen', I will be able to work further. (I copied some code from someone who used CD's but I want to do the same with 'reizen')

This is my code:

extends layout

block content

    #reizen.w3-content.w3-container.w3-padding-64
        h3.w3-center REIZEN

        //reizen    
        input(type='button' onclick='previous()' value='<<')
        input(type='button' onclick='next()' value='>>')
        p
            |
        #showCD Here we will see all the elements of collection 'reizen'

    block content
    script.
        var i = 0;
        var x;
        displayCD(i);
        function displayCD(i) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    myFunction(this, i);
                }
            };
            xmlhttp.open("GET", "cd_catalog.xml", true);
            xmlhttp.send();
        }

        function myFunction(xml, i) {
            var xmlDoc = xml.responseXML;
            x = xmlDoc.getElementsById('showCD');
            document.getElementById('showCD').innerHTML =
            "Title:"  // get the title?
        }

        function next() {
            if (i < x.length-1) {
                i++;
                displayCD(i);
            }
        }
        function previous() {
            if (i > 0) {
                i--;
                displayCD(i);
            }
        }
  • 写回答

1条回答 默认 最新

  • weixin_33724659 2019-12-06 16:54
    关注

    You are trying to use the data server-side. Your AJAX will work client-side. You can use Jade/Pug to create the container to hold your CDs, but you have to actually create the DOM elements client side using createElement, or React, or whatever other DOM API/wrapper you want.

    If you want all of this to be done server side, you don't want to do your AJAX in a script block that gets served to the client, you want to get the CDs programmatically within server-land and then pass the CD data to the Jade/Pug renderer server-side.

    You are trying to mix server-land and client-land and you need to pick one.

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码