如何从 ajax 帖子中更新 A Jade 模板?

发布于 2024-10-21 07:32:47 字数 571 浏览 6 评论 0原文

我已经使用express和默认视图引擎jade设置了一个基本的node.js Web应用程序。

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

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

情况,我无法解决的是如何更改我最初从 ajax 调用传递到 jade 模板中的参数。

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

如果有人有过此工作的经验,我们将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神仙妹妹 2024-10-28 07:32:47

我不太确定您想要什么,但如果它使用 AJAX 调用的结果更新页面(不会刷新或以其他方式重新加载页面),那么您将不得不使用客户端 JavaScript。 jQuery 的 load()post() 应该能够处理这个问题。

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

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

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).

西瓜 2024-10-28 07:32:47

假设您想使用其他“模式”显示同一页面

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

Assuming you want to display the same page, with other "mode"

// 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
  });
});
闻呓 2024-10-28 07:32:47

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

Client

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

Server

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

You could use something like the following, or if you want to use an AJAX call use jQuery ajax as long as there is a response

Client

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

Server

app.get('/ajax', function(req, res){
 //This is where your code and response go
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文