I have a json file and I wanted to display data from that file to HTML. I have written code but while coding it is giving error as 'Type Error: Failed To Fetch'
Here is the code of various files
JSON File
- [
- {
- "id": "1",
- "firstName": "John",
- "lastName": "Doe"
- },
- {
- "id": "2",
- "firstName": "Mary",
- "lastName": "Peterson"
- },
- {
- "id": "3",
- "firstName": "George",
- "lastName": "Hansen"
- }
- ]
- fetch('DisplayData.json')
- .then(function (response) {
- alert('Inside Fetch');
- return response.json();
- })
- .then(function (data) {
- appendData(data);
- })
- .catch(function (err) {
- alert('Error Occured' + err);
- });
-
- function appendData(data) {
- alert('Inside function');
- var m = document.getElementById("mDiv");
- for (var i = 0; i < data.length; i++) {
- var div = document.createElement("div");
- div.innerHTML = 'Name: ' + data[i].firstName + ' ' + data[i].lastName;
- m.appendChild(div);
- }
- }
- <html>
- <script src="DisplayData.js"></script>
- <body>
- <div id= "mDiv">
- </div>
- </body>`enter code here`
- </html>