【问题标题】:I head written node js code but the post method is not working but get method is working, why?我头写了node js代码,但是post方法不起作用但get方法起作用,为什么?
【发布时间】:2021-05-30 15:49:30
【问题描述】:

html部分

<!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Calculator</title>
    </head>
    <body>
        <h1>Calculator</h1>
        <form action="/index.html" method="POST">
            <input type="text" name="num1" placeholder="First Number">
            <input type="text" name="num2" placeholder="Second Number">
            <button type="submit" name="submit">Calculate</button>
        </form>
    </body>
    </html>

节点js部分

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

const app = express();
app.use(bP.urlencoded({extended: true}));

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

app.post("/",function(req,res){
    res.send("Thanks for posting that");
});

app.listen(3000,function(){
    console.log("The server is started on port 3000.");
});

index.html 文件包含两个带有两个数字的输入字段和一个提交按钮。 单击提交按钮时,我得到了输出

无法发布 /index.html

【问题讨论】:

  • 在您的表单中您是否尝试执行action="/index.html"
  • 如果您显示相关的前端代码,人们将能够更好地帮助您。
  • 是的,我已经将 action 属性定义为,action = "/index.html" 仍然 app.post() 不起作用。

标签: html node.js express


【解决方案1】:

我刚刚更正了 app.post() 方法

改正前是这样的:

    res.send("Thanks for posting that");
});```

After correction:
```app.post("/index.html",function(req,res){
    res.send("Thanks for posting that");
});```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2020-03-16
    • 2016-09-04
    • 1970-01-01
    • 2022-01-13
    • 2023-01-01
    • 2020-05-05
    相关资源
    最近更新 更多