【发布时间】:2019-07-31 15:38:39
【问题描述】:
我创建了一个简单的 POST 请求表单来提交电子邮件,但每次我运行它时,它就像一个失败的 GET 请求。
我尝试在邮递员中对其进行测试,但我收到一条消息说“无法得到任何响应”。该表单非常基本,但我在网上找不到任何描述我的问题和有效解决方案的内容。
我已经在我的本地服务器、Heroku 服务器和 Godaddy 共享托管服务器上尝试过这个,所有的行为都是一样的。
我尝试添加不必要的斜线,完全更改 URl,并删除除了 console.log 之外的所有内容。
app.post('/sendEmail', (req, res) => {
console.log('post test');
});
<form method="POST" action="/sendEmail">
<h1>Contact Me</h1>
<hr id="contact-page-hr">
<h4>I am here to serve YOU. Please send your questions my way.</h4>
<h6 id="contact-name-label"><span><input type="text" name="name" placeholder=" Name" value="" id="contact-name-input" required></span></h6>
<h6 id="contact-email-label"><span><input type="email" name="email" placeholder=" Email" id="contact-email-input" required></span></h6>
<h6 id="contact-message-label"></h6>
<textarea name="message" id="contact-message-input" placeholder=" What's on your mind?"></textarea>
<h6 class="sub-heading">(I usually reply within 48 hours)</h6>
<button type="submit" class="send-button" id="contact-page-send">Send</button>
</form>
我希望页面保持不变,但它却显示:
“safari 无法打开页面 'localhost:5000/sendEmail/' 因为服务器意外断开连接...
它本质上就像一个 GET 请求
【问题讨论】:
-
收到请求后如何处理?
-
您的请求处理程序没有发送任何响应。因此,浏览器或服务器最终将超时等待响应的连接。您需要从请求处理程序中
res.send()一些东西。如果您使用 Ajax 调用发送 POST 请求,该页面将保持不变,但如果您执行表单提交,则不会。表单提交需要一个新的 HTML 页面或 302 重定向作为其响应。
标签: node.js express forms http-post