【发布时间】:2021-03-22 18:10:14
【问题描述】:
我有这个 XHR 请求,它将数据从文件夹内的 JSON 文件传递到实际的 <p> 元素,但我无法弄清楚如何传递特定数据,如“标题”或“日期”。
JSON 示例:
{
"title": "Personal development webinar",
"date": "21.04.2021",
"time": "8:00 AM",
"webinar": {
"title": "Personal development webinar",
"link": "https://meet.google.com"
}
}
我的 XHR 请求看起来像这样
function showEvents() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = JSON.parse(xhttp.responseText);
console.log(this.responseText)
}
};
xhttp.open("GET", "./events.json", true);
xhttp.send();
}
【问题讨论】:
-
请向我们展示一个您想要的输出示例,这就是您希望它在带有
id="demo"的元素中出现的方式。 -
是的,我想在我的
Title:
中显示具体数据,例如“Title: Personal development webinar。试图为该解析创建一个 var 并像 var.title 一样使用它,但它不起作用 -
你能显示
JSON.parse(xhttp.responseText);打印到控制台的内容吗?这将有助于准确确定正在发生的事情。 -
我得到了全部 4 个对象的 json 正文
-
imgur.com/a/7Pex66e 这是结果
标签: javascript json xmlhttprequest