Beginner here... I have been trying different methods from answers on this topic but none of them seems to work. I see this question has been asked a lot but somehow i still cant find my answer. I want to use a local json file, that is in the same folder as my JavaScript and HTML file so i can make table from the objects in it and be able to add or remove objects from it. As you already know the commented solution doesn't work. Any help?
HTML File:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" type="text/css" media="screen" href="CSS/style.css" />
</head>
<body>
<button id="json">json</button>
<!-- <script type="text/javascript" src="books.json"></script> -->
<script src="jquery-3.2.1.js"></script>
<script src="script.js"></script>
</body>
</html>
JavaScript File:
$(document).ready(function(){
// var myBooks = JSON.parse(books);
$(document).on('click', '#json', function (event) {
// alert(myBooks[0].author);
// alert(myBooks[0].title);
// alert(myBooks[1].author);
// alert(myBooks[1].title);
});
});
My books.json file
[
{
"author": "Margaret Atwood",
"title": "THE HANDMAID'S TALE"
},
{
"author": "Ursula K. Le Guin",
"title": "THE DISPOSSESSED"
},
{
"author": "Mary Shelley",
"title": "FRANKENSTEIN"
},
{
"author": "Ursula K. Le Guin",
"title": "THE LEFT HAND OF DARKNESS"
},
{
"author": "Connie Willis",
"title": "DOOMSDAY BOOK"
},
{
"author": "Frank Herbert",
"title": "DUNE"
},
{
"author": "Maureen F. McHugh",
"title": "CHINA MOUNTAIN ZHANG"
},
{
"author": "Joe Haldeman",
"title": "THE FOREVER WAR"
},
{
"author": "Kate Wilhelm",
"title": "WHERE LATE THE SWEET BIRDS SANG"
},
{
"author": "Orson Scott Card",
"title": "ENDER'S GAME"
},
{
"author": "Daniel Keyes",
"title": "FLOWERS FOR ALGERNON"
}
]