您好,我正在尝试使用 JQuery 加载本地 JSON 文件以显示数据,但我遇到了一些奇怪的错误。我可以知道如何解决这个问题。
<html>
<head>
<script type="text/javascript" language="javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$.getJSON( "priorities.json" , function( result ){
alert(result.start.count);
});
});
</script></head>
</html>
我只是提醒 JSON 数据的计数。我的 JSON 文件位于该 html 文件所在的同一目录中,JSON 字符串格式如下所示。
{
"start": {
"count": "5",
"title": "start",
"priorities": [
{
"txt": "Work"
},
{
"txt": "Time Sense"
},
{
"txt": "Dicipline"
},
{
"txt": "Confidence"
},
{
"txt": "CrossFunctional"
}
]
}
}
JSON 文件名priorities.json 和错误是
Uncaught Referenceerror priorities is not defined
最佳答案
您可以简单地在 HTML 中包含一个将 JSON 对象声明为变量的 Javascript 文件。然后,您可以使用 data.employees
从全局 Javascript 范围访问您的 JSON 数据,例如。
index.html:
<html>
<head>
</head>
<body>
<script src="data.js"></script>
</body>
</html>
data.js:
var data = {
"start": {
"count": "5",
"title": "start",
"priorities": [{
"txt": "Work"
}, {
"txt": "Time Sense"
}, {
"txt": "Dicipline"
}, {
"txt": "Confidence"
}, {
"txt": "CrossFunctional"
}]
}
}
关于javascript - 尝试使用 JQuery 加载本地 JSON 文件以在 html 页面中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637418/