node.js - 使用expressjs Ejs框架时候,怎么给请求页面发送数据?
大家讲道理
大家讲道理 2017-04-17 13:42:06
0
2
391
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获取的到数据,谢谢!

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
PHPzhong

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.

左手右手慢动作
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!