How to update Jade Template ajax post?

I installed the base node.js web application using express with the default jade view file.

When the user first loads the page, the following happens:

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

What I cannot decide is how then to change the parameter that I originally passed to the jade template from an 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 had experience with this, your input will be appreciated.

+7
source share
3 answers

I'm not quite sure what you need, but if it refreshes the page with the results of an AJAX call (which does not refresh or reload the page), you will have to use client-side JavaScript, jQuery load () or post () should be able to handle this.

Alternatively, if you are not using AJAX, but instead do the usual form submission, you have several options. First, you can save your redirects and use the Express / Connect Session to determine what is used to request a receipt, or two that you can replace by redirecting index.jade from another res.render and include the variable you want to change.

It should be understood that after any of these actions node.js refuses to control the web page in the browser, unless you specifically set up the architecture for communication. There are currently no controls in node.js to force updates to the page or updates to the client. Except for socket connections, or unless otherwise requested by the client itself (for example, in the first example using jQuery).

+4
source

Assuming you want to display the same page with a different "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 }); }); 
+1
source

You can use something like the following, or if you want to use an AJAX call, use jQuery ajax if there is an answer

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 }); 
0
source

All Articles