简体   繁体   English

简单的 POST 请求像 GET 请求一样工作并超时

[英]Simple POST request working like a GET request and timing out

I created a simple POST request form to submit an email, but every time I run it, it acts like a GET request that fails.我创建了一个简单的 POST 请求表单来提交电子邮件,但每次运行它时,它都像一个失败的 GET 请求。

I tried testing it in postman, but I get a message saying "Could not get any response".我尝试在邮递员中对其进行测试,但收到一条消息,提示“无法得到任何响应”。 The form is incredibly basic, but I can't find anything on the web describing my problem and solution that works.该表单非常基础,但我在网上找不到任何描述我的问题和有效解决方案的内容。

I've tried this on my local server, on a Heroku server and a Godaddy shared hosting server, all behaved the same.我已经在我的本地服务器、Heroku 服务器和 Godaddy 共享托管服务器上尝试过这个,所有行为都一样。

I've tried adding unnecessary slashes, changing the URl completely, and taking out everything but a console.log.我尝试添加不必要的斜线,完全更改 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>

I expect the page to stay put, but instead it says:我希望页面保持原样,但它说:

"safari can't open the page 'localhost:5000/sendEmail/' because the server unexpectedly dropped the connection... “safari 无法打开页面 'localhost:5000/sendEmail/' 因为服务器意外断开了连接......

It's essentially acting like a GET request它本质上就像一个 GET 请求

Your request handler does not send any response.您的请求处理程序不发送任何响应。 Thus, the browser or server will just eventually time out the connection waiting for a response.因此,浏览器或服务器最终只会使等待响应的连接超时。

An HTTP request expects a response of some kind. HTTP 请求需要某种类型的响应。 You need to res.send() or res.redirect() something from the request handler.您需要从请求处理程序res.send()res.redirect()一些东西。

The page will stay put only if you send the POST request with an Ajax call, but not if you do a non-Javascript form submission.仅当您使用 Ajax 调用发送 POST 请求时,页面才会保持原状,但如果您执行非 Javascript 表单提交,则不会。 A browser form submission expects a new HTML page or something like a 302 redirect as its response and both client and server are waiting for your code to send that until one of them eventually times out and closes the connection in error.浏览器表单提交需要一个新的 HTML 页面或类似 302 重定向的内容作为其响应,客户端和服务器都在等待您的代码发送它,直到其中一个最终超时并错误地关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM