【问题标题】:POST request not working in nodejs with expressjs and bodyparserPOST 请求在 nodejs 中无法使用 expressjs 和 bodyparser
【发布时间】:2020-12-12 11:59:02
【问题描述】:

我是 nodejs 的新手,目前正在学习。我遇到了我的 POST req 不起作用的问题。我制作了一个简单的 html 表单来执行 GET 和 POST 请求。但是,GET req 工作正常。如何使 POST req 在浏览器上运行或这里有什么问题? 我真的很感谢专家的一些帮助/指导。 谢谢。

const express = require("express");
const bodyParser = require("body-parser");


const app = express();
app.use(bodyParser.urlencoded({ extended: false }));

app.get("/", function(req, res){
    res.sendFile(__dirname + "/index.html");
});

app.post("/", function(req, res){
   
    res.send("thanks for posting");
});
app.listen(3000, function(){
    console.log("the port is working just fine");
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
</head>
<body>
    <h1>Calculator</h1>
    <form action="/" method="post"></form>
    <input type="text" name="num1" placeholder="first number">
    <input type="text" name="num2" placeholder="second number">
    <button type="submit" name="submit">Calculator</button>

</body>
</html>

图像是我的 HTML 和节点 js 代码。

【问题讨论】:

  • 显示 POST 请求和响应
  • 抱歉,没听懂。你能详细说明一下吗? @Anatoly
  • 您在屏幕截图上显示了一个 GET 请求。可以加个POST请求截图吗?
  • 问题是由一个错字引起的:你把&lt;/form&gt;标签放在应该在表单内部的表单控件之前。跨度>

标签: javascript node.js express post get


【解决方案1】:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
</head>
<body>
    <h1>Calculator</h1>
    <form action="/" method="post">
    <input type="text" name="num1" placeholder="first number">
    <input type="text" name="num2" placeholder="second number">
    <button type="submit" name="submit">Calculator</button>
    </form>

</body>
</html>

解决我的问题。

【讨论】:

    猜你喜欢
    • 2017-06-20
    • 2014-12-08
    • 2017-06-16
    • 2018-09-30
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2020-07-04
    相关资源
    最近更新 更多