我在目录中有一个名为“json.json”的json文件。我想从该文件中获取项目并将其显示在我现在已经硬编码的UL中。当我尝试获取该文件时,它不起作用。任何人都可以帮我退休并将这些项目添加到json文件中。在此先感谢从json文件获取数据
<!doctype html>
<html lang="en">
<head>
<title>Example</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script type = "text/javascript" src = "js/jquery.js"></script>
<script type = "text/javascript" src = "js/jquery_ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<table id="myTable" class= "table table-bordered">
<tr>
<th class="col-md-6 text-center success">
List 1
</th>
<th class="col-md-6 text-center danger">
List 2
</th>
</tr>
<tr>
<td class="col-md-6">
<ul id="firstlist">
<li>Apple <img id="Apple" src = "next.jpg" class = "list1"></li>
<li>Orange <img id="Orange" src = "next.jpg" class = "list1"></li>
<li>Avacado <img id="Avacado"src = "next.jpg" class = "list1"></li>
<li>Banana <img id="Banana" src = "next.jpg" class = "list1"></li>
<li>Mango <img id="Mango" src = "next.jpg" class = "list1"></li>
<ul>
</td>
<td class="col-md-6">
<ul class = "seclist" id = "seclist"></ul>
</td>
</tr>
</table>
<script>
$(function(){
$.getJSON('json.json',function(data){
console.log('success');
}).error(function(){
console.log('error');
});
});
</script>
</body>
</html>
我创建的JSON文件是这样的:
{"Fruits":[
{"textToBeDisplayed":"Apple"},
{"textToBeDisplayed":"Orange"},
{"textToBeDisplayed":"Avacado"},
{"textToBeDisplayed":"Banana"},
{"textToBeDisplayed":"Mango"},
]}
抱歉,忘了更改。我也尝试过。我删除了引号,以检查它是否可以在没有引号的情况下工作,并在不改变它的情况下进行复制抱歉。它不能以任何方式工作 – Vithushan
这是对本地文件系统的请求,还是您正在使用Web服务器? –
本地。该json文件位于该文件所在的文件夹中 – Vithushan