【问题标题】:Unable to post the content inside my textArea which is inside the form which sends POST REQUEST无法在发送 POST REQUEST 的表单内的我的 textArea 内发布内容
【发布时间】:2021-08-30 13:27:43
【问题描述】:

HTML
这是我用来向我的服务器发送请求的表单。

<form action="/compose" method="post">
    <div style="margin-bottom: 1rem; margin-top: 2rem;">
        <label for="exampleInputEmail1" class="form-label">Title</label>
        <input type="text" name="postTitle" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
    </div>
    <div>
        <label class="form-label" for="textAreaExample">Post</label>
        <textarea name="postBody" id="textAreaExample" cols="150" rows="10" class="form-control"></textarea>
    </div>
    
    <button type="submit" name="button" class="btn btn-primary" class="mt-4" 
    style="margin-top: 1rem;">Publish</button>
</form

 I am using nodejs to get catch the post request and logging it

在 app.js 中 这里我正在记录发送到服务器的请求

app.post("/compose", function(req, res){
const input = req.body;
console.log(input);
});

问题

Server started on port 3000
{ postTitle: 'dsfds', button: '' }

 But only the postTitle thing is getting logged. Why is that?

【问题讨论】:

    标签: html node.js


    【解决方案1】:

    所以我在这篇文章中找到了解决问题的方法: Here

    我需要做的是将表单 id="composeForm" 添加到我的表单中,然后使用相同的 id 在文本区域内(属性:form="composeForm")

    HTML

    <form action="/compose" method="post" id="composeForm">
    <div style="margin-bottom: 1rem; margin-top: 2rem;">
        <label for="exampleInputEmail1" class="form-label">Title</label>
        <input type="text" name="postTitle" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
    </div>
    <div>
        <label class="form-label" for="textAreaExample">Post</label>
        <textarea name="postBody" id="textAreaExample" cols="150" rows="10" class="form-control" form="composeForm"></textarea>
    </div>
    
    <button type="submit" name="button" class="btn btn-primary" class="mt-4" 
    style="margin-top: 1rem;">Publish</button>
    

    【讨论】:

      猜你喜欢
      • 2012-05-10
      • 2019-06-10
      • 2015-11-03
      • 1970-01-01
      • 2012-09-21
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多