-1

I'am using express-session for creating session in nodejs & using http.post method to send datas to nodejs. How to retrieve the session variable in html using angularjs? Can you please help me?

Here is my code

Inside angularjs controller in the first html page,

   $http.post('/form', data)
       .then(
           function(response){
               if (response.data) {
                   $window.location.href = 'other.html'; 
                }
           }, 
           function(response){
               console.log("inside failure");
           }
        );

In nodejs,

         app.post('/form', function (req, res) {
           console.log("Incoming data" , userData);
           req.session.mob = userData.mob;
           req.session.provider = userData.provider;
           res.send(req.session.mob);
         }
             

I have to display the mob value in the 'other.html' which is stored in nodejs session.Can you suggest me how to do it through angularjs?

1 Answers1

0

Not sure If i understand your question correctly - i'll try to help. First of all, it will be angularJs best practice to use routes or states and pass the data you received from the server (response.data in your example) as a state param or a url param to the new page, and avoid the redirection. If you do need to redirect to the "other.html" just pass the data from the server as query param in the url (if its a property) or save it in the browser storage (local storage, if the new page is being opened in a new tab) and read the data when your other.html is loaded.

Make sure that you pass a url to $window.location.href.

Rot-man
  • 18,045
  • 12
  • 118
  • 124