【问题标题】:Node.js How to get POST queryNode.js 如何获取 POST 查询
【发布时间】:2017-01-31 07:55:23
【问题描述】:

我是 Node.js 的新手,我想知道如何在 html 文件之间“传输”数据,例如:

我得到了带有 2 个文本输入(姓名和年龄)的 submit.html 和一个带有目标 sub.html 的提交按钮,

假设用户提交 name=Alex,age=20,如何在我的 sub.html 脚本(sub.js)中获取这些数据以在 HTML 标签中呈现这些数据?我可能需要使用 socket.io 或 Ajax 吗? node.js中没有一些方法吗?

感谢您回答这些菜鸟问题^^

[编辑] 我忘了说我想要一个非框架解决方案(我在谷歌上没有找到答案)但它似乎不太可能......我将使用节点:)

【问题讨论】:

    标签: javascript node.js post data-transfer


    【解决方案1】:

    您需要使用 expresskoa 等 Web 应用程序框架来处理请求。

    这是使用 express 作为 web 框架来解决上述问题的一种方法:

    app.get('/', function(req, res) {
      // code to render submit.html
    });
    
    app.post('/', function(req, res) {
      // gets value of the submit.html through a ajax request
      // save it in database( such as sqlite, mysql)
    });
    
    app.get('/welcome', function(req, res) {
      // retrieve stored data from database
      // render 2nd html (which displays name and age) using a templating enigne like pug (previously called jade)
    });
    

    【讨论】:

    • 谢谢.. 很遗憾我需要使用一些 freamworks 但它仍然很棒!我还有一个问题:为什么人们不喜欢我的问题?
    • 可能是因为它是一个普遍的问题,而不是特定于 node.js。另外,也许是因为您可以在 google 上轻松找到有关此的博客。
    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多