Javascript 如何将内容传递给客户端html页面中的元素?

Javascript 如何将内容传递给客户端html页面中的元素?,javascript,html,node.js,Javascript,Html,Node.js,我目前正在使用Node.js为一个网页提供服务,该网页接收存储在mongodb服务器上的用户输入。该网页还显示指定的或输入的所有用户输入。我试图弄清楚如何将用户输入从node.js传递到元素 在我的node.js文件中,我将用户数据作为字符串进行响应,如下所示: response.writeHead(200, {"Content-Type": "text/plain"}); response.write(stringifyMongoDBCollection(user_data_collectio

我目前正在使用Node.js为一个网页提供服务,该网页接收存储在mongodb服务器上的用户输入。该网页还显示指定的或输入的所有用户输入。我试图弄清楚如何将用户输入从node.js传递到
元素

在我的node.js文件中,我将用户数据作为字符串进行响应,如下所示:

response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stringifyMongoDBCollection(user_data_collection));
response.end();
当我这样做时,这会重新引导客户端将内容显示为我所期望的
text/plain
。下一步是只更新
的内容。我该怎么做?我想用新填充的
重新提供整个html内容,但这会使所有当前用户输入消失

用户数据将是一个mongodb集合数组,如下所示:

 [ { _id: 5dda17065f7e9b64282e7291,
    date: 'Sat Nov 23 2019 21:37:10 GMT-0800 (Pacific Standard Time)',
    field: '127' },
  { _id: 5dda18ecf330d521a035c444,
    date: 'Sat Nov 23 2019 21:45:16 GMT-0800 (Pacific Standard Time)',
    field: 125},
  { _id: 5dda1951f330d521a035c445,
    date: 'Sat Nov 23 2019 21:46:57 GMT-0800 (Pacific Standard Time)',
    field: '111' } ]

你可以这样做

在节点部分

res.status(200).send(stringifyMongoDBCollection(user_data_collection));
客户端

函数getContent(){ $.ajax({ url:“https://jsonplaceholder.typicode.com/todos", 成功:功能(res){ 如果(res){ res=res.slice(0,5);//将数据限制为5 var-val=''; res.forEach(todo=>{ val+='Title:'+todo.Title+'已完成:'+todo.Completed+'

'; }); } $(“#content”).html(val); }, 错误:函数(){ var val='加载内容时出错

' $(“#content”).html(val); } }); }

得到满足
待办事项

您可以这样做

在节点部分

res.status(200).send(stringifyMongoDBCollection(user_data_collection));
客户端

函数getContent(){ $.ajax({ url:“https://jsonplaceholder.typicode.com/todos", 成功:功能(res){ 如果(res){ res=res.slice(0,5);//将数据限制为5 var-val=''; res.forEach(todo=>{ val+='Title:'+todo.Title+'已完成:'+todo.Completed+'

'; }); } $(“#content”).html(val); }, 错误:函数(){ var val='加载内容时出错

' $(“#content”).html(val); } }); }

得到满足
待办事项

您能否共享此方法的示例数据stringifyMongoDBCollection(user_data_collection)@hbamithkumara在其中添加了一个示例问题,您需要向端点发出Ajax请求。关于它们有很多问题。这能回答你的问题吗?您能否共享此方法的示例数据stringifyMongoDBCollection(user_data_collection)@hbamithkumara在其中添加了一个示例,您需要向端点发出Ajax请求。关于它们有很多问题。这能回答你的问题吗?谢谢你,这就成功了。我确实需要做一些小的改变。我没有使用res.status(200).,因为这是node.js express语法。我最终使用了
response.writeHead(200,{“内容类型”:“text/plain”});write(stringifyMongoDBCollection(用户数据收集));response.end()。在客户端的
函数(res)
中,我有
$(“#content”).html(res)酷。这个例子只是想给出一个想法。谢谢你,这就成功了。我确实需要做一些小的改变。我没有使用res.status(200).
,因为这是node.js express语法。我最终使用了
response.writeHead(200,{“内容类型”:“text/plain”});write(stringifyMongoDBCollection(用户数据收集));response.end()。在客户端的
函数(res)
中,我有
$(“#content”).html(res)酷。这个例子仅仅是为了给出一个想法。