我正在尝试加载看起来像这样的JSON对象
{
"name": "Max",
"age" : "22"
}
按页面上的按钮。
我搜索了stackoverflow和文档,但是并没有真正找到帮助或解决的东西。任何人都可以详细解释如何解决这个问题?提前谢谢。
这是我的index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("test.json", function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button>Klick mich!</button>
<div></div>
</body>
</html>
编辑:好的,所以在Firefox中它确实可以工作并显示名称和年龄。我使用的是Chrome,它没有加载任何内容。
答案 0 :(得分:0)
解决方案是安装XAMPP并设置Apache服务器并将index.html和test.json文件放入htdocs文件夹,这样我就可以在单击按钮时请求json对象。
现在它可以在每个浏览器中按预期工作!