您好我想将解析后的json(我从xmlhttprequest获取)附加到html文件。
这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>alert solr response</title>
<script src="https://code.jquery.com/jquery-3.2.1.js">
</script>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
var json = JSON.parse(xhr.responseText);
var data = json.facet_counts.facet_dates["dc.date.accessioned_dt"];
delete data.gap;
delete data.end;
delete data.start;
console.log(data)
$("#test").append(data); //here is the problem, I could append xhr.Responsetext but this is not what I want
}
}
xhr.open('GET', 'http://localhost:8080/solr/search/select?indent=on&rows=0&facet=true&facet.date=dc.date.accessioned_dt&facet.date.start=2016-01-01T00:00:00Z&facet.date.end=2017-12-01T00:00:00Z&facet.date.gap=%2B1MONTH&q=search.resourcetype:2&wt=json&indent=true', true);
xhr.send(null);
</script>
</head>
<body>
<p id = "test">This is the json sample: <br></p>
</body>
</html>
我知道那里有很多类似的问题,可能有重复,很抱歉我不能用它来做我的情况(作为菜鸟和所有人) 谢谢
答案 0 :(得分:0)
如果你使用jQuery,你可以稍微简化一下,当你获得数据时,你需要使用第三个参数来stringify获取你想要的缩进内容,因为它只是纯文本您还必须将其附加到<pre>
代码或将p标记处的css设置为:white-space: pre
$.get('https://httpbin.org/get').then(function(data) {
delete data.gap;
delete data.end;
delete data.start;
$("#test").text(JSON.stringify(data, null, 2));
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="test"></pre>
&#13;