【发布时间】:2021-12-26 01:02:38
【问题描述】:
这是我想从外部调用的 json 文件,它位于我的 html 文件中,位于一个名为“api”的文件夹中,无法编辑,因为在实时环境中我无法编辑。
{
"tests": [
{
"test_id": "ffc22c5e-1743-49de-9fce-c15df8c15fcc",
"url": "https://www.url1.com",
"checkrate": 300,
"alert_expiration": true,
"alert_errors": true,
"alert_mixed_content": true,
"follow_redirects": true,
"first_expiration_reminder": 30,
"second_expiration_reminder": 7,
"third_expiration_reminder": 1
},
{
"test_id": "52744d25-9492-46b6-999d-af2fcdf87296",
"url": "https://url1.com/",
"checkrate": 86400,
"alert_expiration": true,
"alert_errors": false,
"alert_mixed_content": true,
"follow_redirects": true,
"first_expiration_reminder": 30,
"second_expiration_reminder": 7,
"third_expiration_reminder": 1
},
{
"test_id": "25f171b8-f248-4f63-8e89-ae4813149670",
"url": "https://url3.com/",
"checkrate": 600,
"alert_expiration": false,
"alert_errors": true,
"alert_mixed_content": true,
"follow_redirects": true
}
],
"metadata": {
"total_size": 3
}
}
这是我尝试过的 JS,但在加载 json 时不起作用,我在我的 HTML 页面顶部使用脚本标记。
var json = require('api/ssltests.json'); //(with path)
var ssltests = '';
$.each(json, function(key, value){
if(key === 0) {
ssltests += "<div class=\"row\">";
$.each(Object.keys(value), function(k, v) {
ssltests += "<div class=\"cell\">" + v + "</div>";
});
ssltests += "</div>";
}
ssltests += "<div class=\"row\">";
$.each(Object.keys(value), function(k, v) {
ssltests += "<div class=\"cell\">" + value[v] + "</div>";
});
ssltests += "</div>";
});
【问题讨论】:
-
您必须设置本地服务器。我会推荐 npm 包http-server,它不需要设置。只需在根文件夹中运行
npx http-server。
标签: javascript html jquery json