简体   繁体   English

表单提交后是否重定向?

[英]Redirect after form submit or not?

At the bottom of my personal page (1 page website) I have a contact form that sends a message to my database so I can read it later. 在我的个人页面(网站为1页)的底部,有一个联系表,该表将消息发送到我的数据库,以便以后可以阅读。

The problem is that if I don't redirect to anywhere, the page doesn't reload, which is what I want, but is searching for something and have a loading icon in the tab and eventually disconnects and show the "ECONNRESET" error from Cloud9. 问题是,如果我不重定向到任何地方,页面不会重新加载,这就是我想要的,但是正在搜索内容并在选项卡中具有加载图标,并最终断开连接并显示“ ECONNRESET”错误云9。

If I do redirect back to /#contact (to the page and then down to the contact section), then it reloads the page, and that's not what I want to happen because of the site's functionality. 如果我确实重定向回/#contact(到页面,然后再向下到contact部分),则它将重新加载页面,由于网站的功能,这不是我想要的。 It has some animations that load right after sending the message and the reload cancels them out and it looks terrible. 它具有一些在发送消息后立即加载的动画,重新加载后将其取消,这看起来很糟糕。

How do I make it so that the page doesn't have to reload after sending the message, meaning I don't have to redirect anywhere, but I also don't get the ECONNRESET error? 我如何做到这一点,以便在发送消息后不必重新加载页面,这意味着我不必重定向到任何地方,但是我也不会收到ECONNRESET错误?

app.post("/", function (req, res){
  // Retrieve form data
  var name = req.body.name;
  var email = req.body.email;
  var subject = req.body.subject;
  var message = req.body.message;
  var newMessage = {name: name, email: email, subject: subject, message: message};
  // Save new message to database
  Message.create(newMessage, function (err, sendMessage) {
    if (err) {
      console.log(err);
    } else {
      console.log(sendMessage);
    }
  });
});

If you are not posting the form via AJAX, then you should redirect to the GET endpoint where your view is rendered. 如果您不是通过AJAX发布表单,则应重定向到呈现视图的GET端点。 If you do not want to redirect, then at the very least you will need to have a res.render() call to render the correct view. 如果您不想重定向,那么至少您将需要有一个res.render()调用来呈现正确的视图。 The drawback to not redirecting is that if the user refreshes the page, they may get prompted to resubmit the form. 不重定向的缺点是,如果用户刷新页面,则可能会提示他们重新提交表单。

Message.create(newMessage, function(err, sendMessage){
    if(err){
        console.log(err);
    } else {
        console.log(sendMessage);
        res.render('contact'); //change 'contact' to the name of the view you are rendering
    }
});

I did some searching and I found a way to do it how I want it to work: no redirecting or reloading the page. 我进行了一些搜索,发现了一种方法来实现我的工作方式:无需重定向或重新加载页面。

I removed the unnecessary error handling and console.log since all I need is for the form to send the data to my database so I can read it later. 我删除了不必要的错误处理和console.log,因为我只需要表单将数据发送到我的数据库,以便稍后读取。

app.post("/", function(req, res){
    // Retrieve form data
    var name = req.body.name;
    var email = req.body.email;
    var subject = req.body.subject;
    var message = req.body.message;
    var newMessage = {name: name, email: email, subject: subject, message: message};
    // Save new message to database
    Message.create(newMessage);
});

Apparently I need a Ajax/jQuery request, which looks like this: 显然我需要一个Ajax / jQuery请求,如下所示:

$('form').submit(function(e){
    e.preventDefault();
    $.ajax({
        url:'/',
        type:'post',
        data:$('form').serialize(),
        success:function(){
            //whatever you wanna do after the form is successfully submitted
        }
    });
});

Source to the answer I found (First answer) 我找到的答案的来源 (第一个答案)

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

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