如何更新翡翠模板ajax文章? [英] How do I update A Jade Template form an ajax post?

查看:134
本文介绍了如何更新翡翠模板ajax文章?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

我已经设置了一个基本的node.js web应用程序, / p>

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

我无法解决的是如何将最初传入玉模板的参数从ajax call。

  app.post('/',function(req,res){
console.log req.body.list);
res.redirect('back');
//我想象代码需要去这里,看起来像以下
//
// res.?update-view({
// mode:admin
//});
});

如果任何人有这方面的工作经验,您的输入将不胜感激。

解决方案

我不太清楚你是什么,但是如果用AJAX调用的结果更新页面(不刷新或以其他方式)重新加载页面),那么您将不得不使用客户端JavaScript。 jQuery的 load() post()应该能够处理。



或者,如果您不使用AJAX,而是执行正常表单提交,您有几个选项。一个,您可以保持重定向并使用Express / Connect的会话系统来确定什么是用于获取请求,或两个可以用index.jade页面的另一个res.render替换重定向,并包含要更改的变量。



应该应该理解,在这两个发生之后,node.js将网页的控制权放弃到浏览器中,除非您特别设置了通信架构。 node.js中当前没有控件强制更新或页面更改到客户端。除非通过套接字连接,否则除非由客户端本身进行轮询(例如在第一个涉及jQuery的示例中)。


I have set up a basic node.js web-app using express with the default view engine jade.

When the User first loads the page the following occurs

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

What i cannot work out is how to then change the parameter I initially passed into the jade template from a ajax call.

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"
    // });
});

If anyone has had experience with this working your input would be appreciated.

解决方案

I'm not exactly sure what you're after, but if it's updating the page with the results of an AJAX call (which does not refresh or otherwise reload the page) then you'll have to use client-side JavaScript. jQuery's load() or post() should be able to handle that.

Alternatively, if you are not using AJAX but instead performing a normal form submit, you have a couple of options. One, you can keep your redirect in and use Express/Connect's Sessions system to determine what is used for the get request, or two you can replace the redirect with another res.render of the index.jade page and include the variable you want to change.

It should be understood that after either of these takes place, node.js relinquishes control of the web page to the browser, unless you specifically set up architecture for communication. There are currently no controls in node.js to force updates or page changes down to the client. Except via socket connections or unless otherwise polled by the client itself (such as in the first example involving jQuery).

这篇关于如何更新翡翠模板ajax文章?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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