gpt4 book ai didi

javascript - 我如何仅使用 JavaScript 加载我的本地 json 文件

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:19 25 4
gpt4 key购买 nike

我的 json 文件是这样的

{
"Persons": {
"Name" : "e",
"Name2": "e",
"Id": "4700"
}, [...]
}

我的代码如何将这个本地 json 文件解析/加载到 html 文件中。我什么都试过了,但没有一个奏效。

最佳答案

这是来自 ( http://youmightnotneedjquery.com/) 的示例

request = new XMLHttpRequest();
request.open('GET', '/my/url', true);

request.onload = function() {
if (request.status >= 200 && request.status < 400){
// Success!
data = JSON.parse(request.responseText);
} else {
// We reached our target server, but it returned an error

}
};

request.onerror = function() {
// There was a connection error of some sort
};

request.send();

您的 data 变量将具有如下可访问的成员:

alert(data.Persons.Name);

关于javascript - 我如何仅使用 JavaScript 加载我的本地 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27360326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com