[英]how to load data received from ajax in another page?
I have following pages 我有以下页面
My first page makes ajax call on button click and receives response successfully.. 我的第一页在按钮点击时调用ajax并成功接收响应。
I want to load that data in another page (Page2.html).. 我想在另一个页面(Page2.html)中加载该数据。
$.ajax(
.....
.....
.....
success: function(response){
window.location.href="Page2.html";
$("#content").append(data);
}
);
My div with id of content is on another page(Page2.html)... Note: All my script are in the js file.. 我的内容id的div在另一个页面上(Page2.html)...注意:我的所有脚本都在js文件中。
How can I perform the above task? 我该如何执行上述任务?
Solution I: With Jquery, 解决方案I:使用Jquery,
You need to include Jquery plugin https://github.com/carhartl/jquery-cookie : 您需要包含Jquery插件https://github.com/carhartl/jquery-cookie :
$.cookie("data", data);
Solution II: Using Localstorage. 解决方案II:使用Localstorage。 [Supported in all modern browsers]
[在所有现代浏览器中都支持]
var data = 123;
// Put the var into storage
localStorage.setItem('data', data);
// Retrieve the var from storage
var retrievedData = localStorage.getItem('data');
console.log(retrievedData);
You can use browser local storage which will persist your data on subsequent page visits with no expiration time. 您可以使用浏览器本地存储 ,这将在后续页面访问中保留您的数据,但不会过期。 It has well support in browsers from IE-8 to all modern browsers so it will work without any hassle.
它在从IE-8到所有现代浏览器的浏览器中得到了很好的支持,因此它可以毫无困难地工作。
To store data in you need to access the storage object and set data like this on page1.html
. 要存储数据,您需要访问存储对象并在
page1.html
上设置这样的数据。
$.ajax({
...
...
success: function (result) {
localStorage.setItem('MyData', result);
}
});
On page2.html
, you can retrieve data from localStorage
object using the same key ( MyData
) name we used to set data. 在
page2.html
,您可以使用我们用于设置数据的相同密钥( MyData
)名称从localStorage
对象检索数据。 You can choose any meaningful name for this. 您可以为此选择任何有意义的名称。
var data = localStorage.getItem('MyData');
console.log("use data wherever you want...", data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.