Sekhar Sekhar - 5 days ago 5
Node.js Question

Retaining a value from a response in node js

I am facing a challenge dealing with the below scenario: -

On page load i.e., for a particular route for e.g.,

https://localhost:8000/home/index
a service is called and the response from the service is rendered to the page at the client side.

On the same page, i have a link that pops up a backbone.js modal and from the modal a click event triggers which hits another url for e.g.,
https://localhost:8000/home/index2
upon which another service call triggers and the reponse is rendered to another html page.

On the same html page, i want to display a value which i got from the first service call on page load. However, i am unable to retain that value as there are two different requests each time. hence, i cannot even append the value from first response to the request object and use it second time.

Any help on this?

Please let me know if you need any more clarity.

Answer

You can use JavaScripts Web Storage API to storage information on client browser.

MDN Web Storage API

For example, If you are on the first screen and call a service, store the service information on localStorage

localStorage.setItem('firstService', serviceResponseObject);

Once you are navigated to second page, you can use localStorage to read to previous service information

localStorage.getItem('firstService');

Comments