快速使用AJAX(请求后更改variables)

我想制作一个单页的应用程序。 简化我想要的:首先在"/"将是一个带有2个button的显示部分的主页。 当他们点击button1时,我们将从服务器中检索数据,并将其显示在显示部分的页面上,对button2做同样的事情,但对于不同的数据。

我很难抓住如何把正确的显示部分。

我知道你可以在翡翠做条件:

  html head script(src ="http://code.jquery.com/jquery-2.1.1.js") script(src = "script.js") body p= test if test == "face" div.fillIn(style="background:blue; width:400; height:200") if age div.fillIn(style="background:red; width:400; height:200") button(value="send").send send button(value="change").change change 

我如何在年龄和testing中join不同的variables? 在服务器中,我无法在发送请求后更改variables。 应该试图做到这一点?

 var express = require("express"); var app = express(); var bodyParser = require("body-parser"); var name; var age; app.use(bodyParser.urlencoded({extended : true})); app.use(bodyParser.json()); app.use(express.static("public")) app.set("view engine", "jade") app.get("/", function(req, res){ // if(age){ res.render("index",{test: "fac", age: age }) // } }) //The server script that reads the paramater from the jQuery AJAX and returns the result app.post("/some", function(req, res){ console.log(req.body) age = req.body.age res.send(req.body.name + " and " + req.body.age) }) ...... 

(我正在考虑这样做)。我想这样做,所以当button被点击时,它会检索新的数据,它会更新一个variables,所以我可以把它传递给Jade,所以它可以更新正确的显示

button的东西在script.js

  $("button.send").on("click", function(){ $.ajax({ method : "POST", url : "/some", data : {name : Math.floor(Math.random() * 20), age :"42"}, success : function(data){ $(".fillIn").html(data) } }) 

编辑:我想做的是有2个不同的AJAX调用2个不同的button,每个输出2个不同的数据,如上面的data : {name : Math.floor(Math.random() * 20), age :"42"}另一个是data : {name : Math.floor(Math.random() * 20), age :"--->43<---"} 。 这是什么,我认为将在填写.fillin文本div但我不想只填写div我想要一个其他的fillin div。 一个button将导致该div有其他的特定文本(HTML),用户可以读取数据和另一个button将与其他HTML元素和特定的数据相同。 我希望有一种方法来传递模板。 这就是我想通过有2个条件div来模拟。 在这一点上,我并没有试图为特定的用户做点什么。 我只是想,如果我可以做的data : {name : Math.floor(Math.random() * 20), age :"42 | 43"},两个button。 我传递一个很长的数据,我想说哪个div显示。