Express.js不会在后期行动中呈现 [英] Express.js Won't Render in Post Action

查看:133
本文介绍了Express.js不会在后期行动中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js有点新鲜,所以我几乎无法在互联网上找到东西。
我已经解决了这个问题。这是因为我使用 $。ajax 从客户端打电话。
但是当我在下面的代码中尝试时,它不会呈现。
初始页面是/ contact,当我打电话/ contact / sendEmail时,/ contact / sendEmail里面的所有函数都正在工作,但没有渲染。
根本没有错误。没有任何事情发生。

  var Contact = require('../ controllers / contact'); 

module.exports = function(app){
app.get('/ contact',function(req,res){
res.render('contact');
});
app.post('/ contact / sendEmail',function(req,res,next){
var form = req.body;
Contact.validators.form(form,function(err ){
if(err){
return res.render('user / register'); //这一个从来没有工作!
/ * return res.render('contact',{
error:err,
}); * /
} else {
Contact.sendEmail(req,res,next);
}
}) ;
});
};

这个函数调用了



<$ p $($)$($)$($)$($)$($)$($) ;
//服务器端验证
//每个联系表单字段都是必需的
event.preventDefault();
$ .ajax({
type:'POST' ,
url:'/ contact / sendEmail',
data:form.serialize()
})
.done(function(data){
if(data .status =='OK'){
$('#contact-success')。modal('show');
} else {
return done('请填写所有字段',false);
}
});
});

让大家学习。
我将其从服务器端更改为通过将其添加到玉器文件中:

  form(role =form method =POST)


解决方案

我成功解决了我自己!
我发现这是因为我从客户端从 $。ajax 调用路由。
当我通过将代码放入表单并删除 $。ajax 来更改调用方法时,已经完成了。谢谢@mscdex

  form(role =formmethod =POST)

,我将路由 / contact / sendEmail 更改为 / contact


Node.js is kinda new, so I hardly can find things in internet. I have already solved this. It was because I was calling from client side using $.ajax. But when I try it in my code below, it never renders. The initial page was /contact, and when I call /contact/sendEmail, all of the function inside /contact/sendEmail are working but not with the render. There was no error at all. It was just like nothing happened.

var Contact = require('../controllers/contact');

module.exports = function(app) {
  app.get('/contact', function(req, res) {
    res.render('contact');
  });
  app.post('/contact/sendEmail', function(req, res, next) {
    var form = req.body;
    Contact.validators.form(form, function(err) {
      if (err) {
        return res.render('user/register'); //This one never works!
        /*return res.render('contact', {
          error: err,
        });*/
      } else {
        Contact.sendEmail(req, res, next);
      }
    });
  });
};

It was called by this function

$('.contact').submit(function(event, done) {
  var form = $(this);
  var data = form.serializeArray();
  // server side validation
  // every contact form field is required
  event.preventDefault();
  $.ajax({
    type: 'POST',
    url: '/contact/sendEmail',
    data: form.serialize()
  })
  .done(function(data) {
    if (data.status == 'OK') {
      $('#contact-success').modal('show');
    } else {
      return done('Please fill all of the fields.', false);
    }
  });
});

For everybody to learn. I changed it to call from server side by adding this to the jade file:

form(role="form" method="POST")

解决方案

I successfully solved it by myself! I found out that it happened because I call the route from $.ajax from the client. When I change the calling method by putting this code into the form and remove the $.ajax, it's done. Thank you @mscdex

form(role="form" method="POST")

and I changed the route /contact/sendEmail to just /contact.

这篇关于Express.js不会在后期行动中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆