我如何更新翡翠模板forms的ajax文章?

我已经使用默认的视图引擎玉来build立一个基本的node.js web应用程序。

当用户第一次加载页面时,会发生以下情况

app.get('/', function(req, res){ res.render('index', { title: 'Test', mode: "user" }); }); 

我不能解决的是如何改变我最初通过ajax调用进入玉模板的参数。

 app.post('/', function(req, res){ console.log(req.body.list); res.redirect('back'); // I imagine the code needs to go here and look somewhat like the following // // res.?update-view({ // mode: "admin" // }); }); 

如果有人有这方面的工作经验,您的意见将不胜感激。

我不完全确定你在做什么,但是如果它正在更新AJAX调用的结果(不刷新或重新加载页面)的结果,那么你将不得不使用客户端JavaScript。 jQuery的load()或post()应该能够处理。

另外,如果你不使用AJAX,而是执行一个正常的表单提交,你有几个选项。 一,你可以保持你的redirect,并使用Express / Connect的会话系统来确定什么是用于获取请求,或者你可以用另一个res.renderreplaceredirect到index.jade页面,并包含你想要的variables更改。

应该明白,在发生这两种情况之后,除非您专门设置了通信架构,否则node.js会将网页的控制权交给浏览器。 目前在node.js中没有任何控件强制更新或页面更改到客户端。 除了通过套接字连接,或除非由客户端本身进行轮询(例如在涉及jQuery的第一个示例中)。

假设你想显示相同的页面,与其他“模式”

 // set the page title for all requests app.locals({ title: 'Test' }); // GET request app.get('/', function(req, res){ res.render('index', { // displays the default "user" mode mode: 'user' }); }); // when POST is submited app.post('/', function(req, res){ // this is the param given by the user console.log(req.body.list); // now render the same page res.render('index', { // with mode set to the the given parameter mode: req.body.list }); }); 

你可以使用类似下面的内容,或者如果你想使用AJAX调用,只要有响应就使用jQuery ajax

客户

 script(type="text/javascript") $(document).ready(function(){ //...AJAX here.... var newValue = #{value} + 10; }); 

服务器

 app.get('/ajax', function(req, res){ //This is where your code and response go });