我有一个简单的表单,可将​​用户输入发布到服务器。提交表单后,页面加载时间很长,从而导致ERR_EMPTY_RESPONSE。但是,我可以从表单输入console.log很好,以验证表单确实已发布到服务器。

这是我正在使用的:

<form method="post" action="/">
    <input type="text" name="userZipcode" placeholder="Enter your zip code">
    <button type="submit">ENTER</button>
</form>




app.use(bodyParser.urlencoded({
    extended: false
}));

app.use(bodyParser.json());

app.get('/', (req, res, next) => {
    fetch(url)
    .then(res => res.json())
    .then(data => res.render('pages/index'))
    .catch(err => {
        console.log(err);
        res.sendStatus(500);
    });
});

app.post('/', function(req, res) {
    let zipcode = req.body.userZipcode;
    console.log('Your location is: ' + zipcode);
});


注意:提取请求最终将把数据从API响应传递到我的EJS页面,但是目前它只呈现索引页面而已。

上面的表格有效,我按预期从终端的输入字段中获得一个带有邮政编码的console.log。但是,此后,页面只会加载,直到以上述错误结束为止。我知道我错过了重要的事情,但是我不知道那是什么!

最佳答案

在发布路线中,您必须做出回应。您的代码未响应前导ERR_EMPTY_RESPONSE的请求。

app.post('/', function(req, res) {
    let zipcode = req.body.userZipcode;
    console.log('Your location is: ' + zipcode);
    res.end("your response")
});

关于javascript - POST到Node.js服务器导致长负载,从而导致ERR_EMPTY_RESPONSE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48717524/

10-12 12:25
查看更多