Session Storage in js

前端 未结 3 1028
死守一世寂寞
死守一世寂寞 2021-02-14 18:06

Hi i\'m a beginner in JavaScript i would like to ask if its possible to session the data i\'m getting from a server. The data i want to session is the \'data.xhr.response\' i se

3条回答
  •  Happy的楠姐
    2021-02-14 18:49

    Session storage is same as local storage but the only difference is that data stored in session storage will clear automatically once page session will expire.

    Now come to your question. In your success function, do like below to store data:

    success : function(data) {
        console.log(data.xhr.response);
        sessionStorage.setItem('dataStored', data.xhr.response);
    
    }
    

    Usage is like below:

    // Save data to sessionStorage
    sessionStorage.setItem('dataStored', data.xhr.response);
    
    // Get saved data from sessionStorage
    var data = sessionStorage.getItem('dataStored');
    
    // Remove saved data from sessionStorage
    sessionStorage.removeItem('dataStored');
    
    // Remove all saved data from sessionStorage
    sessionStorage.clear();
    

    For more information about sessionStorage check this link : https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

提交回复
热议问题