www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] node.js redirect and post

  1. #1
    Join Date
    Jul 2013
    Posts
    16

    resolved [RESOLVED] node.js redirect and post

    I have an app I made in node.js and it's posting data, mostly from a table, to the server using jquery's post method. The request is handled in app.js and it responds with an html string.

    Example client-side code:
    Code:
    $.post(
      form.attr("action"), //this is the url the form is posting to
      data, //this is an array of data made by using form.serializeArray() and pushing data from tables into it
      function(data){ alert(data) } //this is the success function that alerts the user with the response
    );
    Example server-side code:
    Code:
    app.post("/url", function(req, res){
      ...
      var name = body.request.name;
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write('Thank you, '+name+', your data has been posted.');
      res.end();
    });
    This works great, but what I would like to do is redirect the user to the url the form is posting to and display the message on that webpage. I can get it to redirect to the url inside the success function, but I can't get the response message written to it. Does anyone know how to do this?

  2. #2
    Join Date
    Jul 2013
    Posts
    16
    I figured out the solution involves either using session variables or writing to the page you are redirecting to, using the node.js file system module. Express/connect makes session variables pretty easy, but I don't think it's necessary to use them unless you are tracking a user login. Some people think it's a good idea to show the response in the post url, but refreshing that url will re-post to the server. Best practice is to use the Post-Redirect-Get method, and disable the submit button after it is clicked.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles