Skip to content

Instantly share code, notes, and snippets.

@ksmarty
Last active February 8, 2020 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksmarty/266d7b8cf3ef0970e447e3f6a1eb7444 to your computer and use it in GitHub Desktop.
Save ksmarty/266d7b8cf3ef0970e447e3f6a1eb7444 to your computer and use it in GitHub Desktop.
// 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