你好我想附加解析的json(我從xmlhttprequest獲得)到一個html文件。附加解析的json到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>
我知道有很多在那裏類似的問題和可能的重複,我很抱歉,我不能用他們對我的情況下(作爲一個小白和所有) 謝謝
字符串化並不好看,是很難讀,我想在不同的行各個領域,如JSON –
'JSON.stringify(OBJ,空,2);'[進入滿足神](https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript) –
只是一個提示,如果你使用jQuery,[$ .get()]( https://api.jquery.com/jquery.get/) – Endless