nodejs redirecting data from ajax to ejs

481
April 01, 2017, at 05:14 AM

I'm using ajax post on client side to post the user details to database. On server side, i handle my post request as :

      app.post("/journey",function(req,res){
      var obj = JSON.stringify(req.body);
      var dt = JSON.parse(obj);
      //console.log(dt.zip)
      var journey_details = new Journey({
        zip : dt.zip,
        age65 : dt.age65,
        first_name : dt.fname,
        last_name : dt.lname
      });
      journey_details.save(function (err) {
     if (err) {
       return err;
     }
     else {
       console.log("Journey details saved");
       // i use some api here to get json data named parsedData
       //since i've to display the name and parsedData on a new page, i send the data to front end javascript as 
       res.send({redirect: '/profile', first_name : dt.fname, last_name : dt.lname, parsedData :  parsedData});
    });
    });

Since i'm using ajax post, i redirect on ajax success. Once i get the above data, i redirect it to a new profile ejs page using

    var final_url = "http://localhost:3000" + data.redirect +"/"+data.first_name + "/" + data.last_name;
    window.location.replace(final_url);

which will redirect them to localhost:3000/profile/fname/lname or may be i can use some unique id or anything like that in url and it is handled in my profile ejs page

But i'd also want to send the parsedData to my profile ejs page. Since. parsedData has multiple json object with many fields, i don't think i can send it via url as :

    var final_url = "http://localhost:3000" + data.redirect +"/"+data.first_name + "/" + data.last_name + "/"+data.parsedData;
    window.location.replace(final_url);

Is there any way so that i scan send it other than as in urlheader so that as i can access the parsedData as easily as i access fname and lname below:

    app.get("/profile/:firstname/:lastname",function(req,res){
    console.log(req.params.firstname);
    res.render("profile", {firstname : req.params.firstname,lastname : req.params.lastname}) // i want parsedData here
  });
Answer 1

GET HTTP requests are not intended for the big url data, but using POST request in this case is logical error. Maybe the best way for you would be to pass only your journey_details.id to redirect url, without any data. And on the server side you will get this record and use all data you need.

Rent Charter Buses Company
READ ALSO
mongoose query same field with different values

mongoose query same field with different values

Is there a way to user mongoosefind({title:'some title'}) to query the same field with multiple values? For example something like this mongoose

317
How to map domain name to Websocket server (ws) nodejs?

How to map domain name to Websocket server (ws) nodejs?

I have been working on websocket implement, i just want to host it to public domainI have domain "www

480
Jasmine-node expect not executed

Jasmine-node expect not executed

I started testing today with Jasmine, which is new to meSince I am working with a GraphQL-server, developed with NodeJS, I am using jasmine-node

396
Node.js passport sign up

Node.js passport sign up

I am building a mobile only back-end with Nodejs utilizing Passport version 0

623