A company that I am working with has an API for their Privacy Policy and Terms of Use. I need to create two different pages...one for the Privacy Policy and one for the Terms of Use. I'm pretty new at this, and I feel like I'm so close.
我正在与之合作的公司拥有其隐私政策和使用条款的API。我需要创建两个不同的页面...一个用于隐私策略,一个用于使用条款。我对此非常陌生,我觉得我很亲密。
Here is the way that the JSON is received (with some edits to anonymize it):
以下是接收JSON的方式(通过一些编辑对其进行匿名化):
{"code":200,"status":"Ok","data":
{"offset":0,"limit":20,"total":2,"target":"html_page","results":[
{
"id":"6",
"title":"Privacy Policy",
"description":"Privacy Policy",
"html":"HTML CODE BLAH BLAH",
"tags":["privacy"]
},
{
"id":"66",
"title":"License and TOU",
"description":"Terms of Use",
"html":"HTML CODE BLAH BLAH",
"tags":["terms"]
}]
}
}
Here's the code I'm using:
这是我正在使用的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Privacy Policy</title>
</head>
<body>
<div id="placeholder"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$.getJSON('http://externalurl.com', function(external) {
var output="<ul>";
for (var i in external.code.offset) {
output+="<li>" + external.html + "</li>";
}
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
});
</script>
</body>
</html>
It's just returning a blank page. What am I doing wrong?
它只是返回一个空白页面。我究竟做错了什么?
2 个解决方案
#1
3
$.getJSON('http://externalurl.com', function(external) {
var output = $("<ul />");
$.each(external.data.results, function(i, result) {
$('<li />', {text : result.html}).appendTo(output);
});
$('#placeholder').html(output);
});
#2
0
I think it should be:
我认为它应该是:
$.getJSON('http://externalurl.com', function(external) {
var output="<ul>";
for (var i = 0; i < external.data.results.length; i++) {
output+="<li>" + external.data.results[i].html + "</li>";
}
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
});
So first you are iterating over the wrong object, it's external.data.results
and not external.code.offset
. Second, you can't iterate over an array with for (var i in array)
.
所以首先你要遍历错误的对象,它是external.data.results而不是external.code.offset。其次,您无法使用for(数组中的var i)迭代数组。
#1
3
$.getJSON('http://externalurl.com', function(external) {
var output = $("<ul />");
$.each(external.data.results, function(i, result) {
$('<li />', {text : result.html}).appendTo(output);
});
$('#placeholder').html(output);
});
#2
0
I think it should be:
我认为它应该是:
$.getJSON('http://externalurl.com', function(external) {
var output="<ul>";
for (var i = 0; i < external.data.results.length; i++) {
output+="<li>" + external.data.results[i].html + "</li>";
}
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
});
So first you are iterating over the wrong object, it's external.data.results
and not external.code.offset
. Second, you can't iterate over an array with for (var i in array)
.
所以首先你要遍历错误的对象,它是external.data.results而不是external.code.offset。其次,您无法使用for(数组中的var i)迭代数组。