无法打印 Json 对象
Unable to print Json object
下面的代码在循环中从 URL 获取 Json 对象。
问题是我似乎无法显示返回的 Json 数据。
我想显示物体的温度和湿度。
有效 Json 对象返回正常,但我无法在 HTML div 中显示它。
我没有看到屏幕上打印任何内容。
完整代码:
<html>
<head>
<title>json loop</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<div id="container">
<div id="div"></div>
<div id="output">nothing yet</div>
</div>
<script>
var previous = null;
var current = null;
var data;
setInterval(function() {
$.getJSON(
"https://dweet.io/get/latest/dweet/for/myesp8266",
function(json) {
data = json;
current = JSON.stringify(data);
$("div").html(data);
console.log(data);
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
});
}, 2000);
var output = document.getElementById('output');
output.innerHTML = data ;
</script>
</body>
</html>
你试过这样吗?
setInterval(function() {
$.getJSON("https://dweet.io/get/latest/dweet/for/myesp8266",
function(json) {
data = json;
current = JSON.stringify(data);
//$("div").html(data);
console.log(data);
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
var output = document.getElementById('output');
output.innerHTML = current ;
});
}, 2000);
编辑:
这里有一个关于它如何工作的例子:https://plnkr.co/edit/GI5uzojOOmJNwAc73aXq?p=preview
您没有将字符串化的字符串放入 HTML。使用 current
而不是 data
?
data = json;
current = JSON.stringify(data);
$("div").html(current);
console.log(current);
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
编辑
如果你想要温度,使用:
$("div").html(data.with[0].content.temperature);
下面的代码在循环中从 URL 获取 Json 对象。
问题是我似乎无法显示返回的 Json 数据。 我想显示物体的温度和湿度。
有效 Json 对象返回正常,但我无法在 HTML div 中显示它。
我没有看到屏幕上打印任何内容。
完整代码:
<html>
<head>
<title>json loop</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<div id="container">
<div id="div"></div>
<div id="output">nothing yet</div>
</div>
<script>
var previous = null;
var current = null;
var data;
setInterval(function() {
$.getJSON(
"https://dweet.io/get/latest/dweet/for/myesp8266",
function(json) {
data = json;
current = JSON.stringify(data);
$("div").html(data);
console.log(data);
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
});
}, 2000);
var output = document.getElementById('output');
output.innerHTML = data ;
</script>
</body>
</html>
你试过这样吗?
setInterval(function() {
$.getJSON("https://dweet.io/get/latest/dweet/for/myesp8266",
function(json) {
data = json;
current = JSON.stringify(data);
//$("div").html(data);
console.log(data);
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
var output = document.getElementById('output');
output.innerHTML = current ;
});
}, 2000);
编辑:
这里有一个关于它如何工作的例子:https://plnkr.co/edit/GI5uzojOOmJNwAc73aXq?p=preview
您没有将字符串化的字符串放入 HTML。使用 current
而不是 data
?
data = json;
current = JSON.stringify(data);
$("div").html(current);
console.log(current);
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
编辑
如果你想要温度,使用:
$("div").html(data.with[0].content.temperature);