This shows how to load data defined in a json resource into a local variable, in JavaScript code.
The snippet.js loads the content of a baroData.json resource into a variable named graphData.
Enjoy!
| const URL = './baroData.json'; | |
| var graphData; | |
| fetch(URL) | |
| .then(response => { | |
| console.log(`Response: ${response.status} - ${response.statusText}`); | |
| response.json().then(doc => { | |
| graphData = doc; | |
| console.log(`PRMSL data loaded, ${doc.length} elements`); | |
| // Process it here if needed | |
| // . . . | |
| }); | |
| }, | |
| (error, errmess) => { | |
| console.log("Ooch"); | |
| let message; | |
| if (errmess) { | |
| let mess = JSON.parse(errmess); | |
| if (mess.message) { | |
| message = mess.message; | |
| } | |
| } | |
| console.debug("Failed to get PRMSL data..." + (error ? JSON.stringify(error, null, 2) : ' - ') + ', ' + (message ? message : ' - ')); | |
| }); |