Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Text File
fetch('file.txt')
.then(response => response.text())
.then(text => console.log(text))
// ----------------------------------------------------------------------------
// JSON File
fetch('file.json')
.then(response => response.json())
.then(jsonResponse => console.log(jsonResponse))
// ----------------------------------------------------------------------------
// XML File
fetch('file.xml')
.then(response => response.text())
.then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
.then(data => console.log(data))
document.getElementById("json").innerHTML = JSON.stringify(json, undefined, 2);
// JSON.stringify(JSON data, replacer, spacing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment