我在目錄中有一個名爲「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