Home  >  Q&A  >  body text

node.js - 使用expressjs Ejs框架时候,怎么给请求页面发送数据?

router.get('/', function(req, res, next) {
    res.render('demo5.html');//渲染的页面
});
 
router.post('/', function(req, res, next) {
    res.send("要发送的数据");//要发送的数据
});

我在demo5.html中用ajax post请求发送表单数据后,想返回给原来的页面数据让ajax获取到数据,上面代码倒数第2行执行完直接转向如下图

怎么做才能使原来的页面不变又能让ajax获取的到数据,谢谢!

大家讲道理大家讲道理2765 days ago396

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 13:44:06

    I wrote it according to your method. res.send() will not cause the page to jump. If I guessed correctly, your data is placed in form. Submitting the form will cause the page to jump, and then the server will If the results in app.post() in res.send() are returned to the new page, the problem you mentioned will occur. Just use ajax to submit data and there will be no problem.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:44:06

    router.get('/', function(req, res, next) {
        res.render('demo5.html',{name:xxx});//渲染的页面
    });

    This is the value-passing method of get

    You can call {{name}} to

    on ejs

    reply
    0
  • Cancelreply