【问题标题】:Cannot POST / With Post request from non '/' site无法 POST / 来自非“/”站点的 Post 请求
【发布时间】:2021-05-08 23:39:19
【问题描述】:

我是 Node 新手,在使用 POST 功能时遇到问题。 我有一个简单的网站,其中有以下几行:

app.get('/sign',(req,res)=>{
    res.sendFile(path.join(__dirname, 'static', 'index.html'));
});

app.post('/sign', (req,res)=>{
    res.send("Success!")
});

get 请求工作得很好,但是当我将表单中的数据发回时,我没有收到成功消息,而是收到了Cannot POST /。 当使用 app.get('/', etc.) 从“/”目录执行相同操作时 它工作正常。

这是“index.html”文件的内容:

<form action = '/' method='POST' id="form">
    <div class="form-group">
        <label for="emal">Email address</label>
        <input type="email" class="form-control" name="email">
    </div>
    <div class="form-group">
        <label for="pwd">Password</label>
        <input type="password" class="form-control" name="password">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

【问题讨论】:

  • 您能否分享您用于“将表单中的数据发回”的代码?您似乎没有发布到正确的端点。

标签: html node.js http web post


【解决方案1】:

action 属性指定表单数据将发布到的 URL。

你说过要发到/

您的服务器端代码为/sign 提供路由,但不为/ 提供路由。

您需要匹配您的网址。要么将 action="/" 更改为 action="/sign" 以明确说明您将其发布到的位置 要么 完全删除 action 属性,以便它发布到 当前 url(其中是/sign,因为这是提供包含表单的 HTML 文档的 GET 处理程序的路由。

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 2014-12-23
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2020-07-01
    • 2013-12-07
    • 1970-01-01
    相关资源
    最近更新 更多