希望检索并显示存储在本地存储中的数据。我试过通过很多方式显示它,比如console.log,alert,在css中显示。但是该方法都不会显示从本地存储中检索到的数据。
代码:
<!DOCTYPE html>
<html>
<head>
<title>Pricing </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<meta http-equiv="refresh" content="10" />
<script>
function requestCrossDomain(site, callback) {
if (!site) {
alert('No site was passed.');
return false;
}
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';
$.getJSON(yql, cbFunc);
function cbFunc(data) {
if (data.results[0]) {
data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
window[callback](data);
} else throw new Error('Nothing returned from getJSON.');
}
}
var url = 'https://www.emcsg.com/marketdata/priceinformation';
requestCrossDomain(url, 'someFunction');
function someFunction(results){
var html = $(results);
var table = html.find(".view72PeriodsWrapper");
var table_pre = html.find(".view72PeriodsWrapper").find(".previous").text();
var table_cur = html.find(".view72PeriodsWrapper").find(".current").text();
var table_fut = html.find(".view72PeriodsWrapper").find(".future").text();
$('#loadedContent').css("display","").html(table);
var jsonObject = JSON.stringify(table_pre);
var jsonObject2 = JSON.stringify(table_cur);
var jsonObject3 = JSON.stringify(table_fut);
jsonObject = jsonObject.concat(jsonObject2, jsonObject3);
$('#json').text(jsonObject);
window.localStorage.setItem('jsonobject', jsonObject);
var retrievedObject = window.localStorage.getItem('jsonobject');
var data = JSON.parse(retrievedObject);
console.log(data); //The data I want to display
}
</script>
</head>
<body>
<br><br>
<div id="result"></div>
<div id="loadedContent"></div>
<div>JSON OBJECT <br></div>
<div id ="json"></div>
<div id ="data"></div>
</body>
</html>
我该怎么做才能显示数据?
P.S:网页编码为每10秒自动刷新一次。因此json对象应每10秒存储在本地存储中。我想显示它以确保它按照我的要求存储
答案 0 :(得分:1)
请使用如下的localStorage。
//To set a value in LocalStorage
localStorage.setItem('key', 'value');
//To get a value from LocalStorage
localStorage.getItem('key')
答案 1 :(得分:0)
我认为你对本地存储有错误的套管。
window.localStorage.setItem('jsonobject', jsonObject);
答案 2 :(得分:0)
你错过了localStorage
的骆驼案例,
localstorage.setItem('jsonobject', jsonObject);
var retrievedObject = localstorage.getItem('jsonobject');