Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Json in javascript einlesen
#1
I have one json-file and one html-file
C:/Users/usr/Test.json
and
C:/Users/usr/Test.html
. The entire website is used only local on my computer. Test.html is written once, Test.json is constantly overwritten. Test.json is essentially a table: "{[{"key1": "value1"}, {"key2": "value2"}]}".

I now need the Test.html to read in that Test.json.

I used following code, which does not work.
The html-file
<html>
<head>
<script type="text/javascript">
fetch('./Test.json')
    .then((response) => response.json())
    .then((json) => console.log(json));
</script>

</head>

<body>

<table id= "userdata" border="0">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>City</th>
</thead>
<tbody>
</tbody>
</table>

</body>

<script>
   
</script>
</html>

The json-file
[
{"firstName": "Test2", "lastName": "Test1", "job": "Test2", "roll": "Haha"},
{"firstName": "Test2", "lastName": "Test1", "job": "Test2", "roll": "Haha"}
]

How to fix that?


Angehängte Dateien Thumbnail(s)
   
Zitieren
#2
Hallo and welcome to the forum!
The first error message says it all: The file protocol is not among the protocols being supported. In order to fix this, you need to load your HTML file through a webserver. Either install one locally (e. g. XAMPP) or upload the files to your webspace and test over there.
Good luck!
Glaube denen, die die Wahrheit suchen, und zweifle an denen, die sie gefunden haben.
(Andrι Gide (1869-1951), frz. Schriftst., 1947 Nobelpreis)
Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen:
1 Gast/Gäste