js中的会话存储 [英] Session Storage in js
问题描述
您好,我是JavaScript的初学者,我想问一下是否有可能对我从服务器获取的数据进行会话.我要会话的数据是我在网上搜索的"data.xhr.response",大多数人都使用SessionStorage函数,但我不确定如何处理它,任何建议都将不胜感激.
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 searched online that most people use the SessionStorage function but i'm not really sure how to go about it any suggestion would be appreciated.
$(function()
{
Dropzone.autoDiscover = false;
$('#file-upload').dropzone({
maxFiles: 1,
acceptedFiles: ".pdf,.doc,.docx,.html",
dataType: "json",
success : function(data) {
console.log(data.xhr.response);
}
});
});
推荐答案
会话存储与本地存储相同,但唯一的区别是,一旦页面会话到期,会话存储中的数据将自动清除.
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);
}
用法如下:
// 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();
有关 sessionStorage
的更多信息,请检查以下链接: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/sessionStorage
For more information about sessionStorage
check this link : https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
这篇关于js中的会话存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!