How to read an external local JSON file in JavaScript?

You cannot make a AJAX call to a local resource as the request is made using HTTP. A workaround is to run a local webserver, serve up the file and make the AJAX call to localhost. In terms of helping you write code to read JSON, you should read the documentation for jQuery.getJSON(): http://api.jquery.com/jQuery.getJSON/

await is only valid in async function

The error is not refering to myfunction but to start.  Run code snippetExpand snippet I use the opportunity of this question to advise you about an known anti pattern using await which is : return await. WRONG  Run code snippetExpand snippet CORRECT  Run code snippetExpand snippet Also, know that there is a special case where return await is correct and important : (using … Read more

Unexpected token u in JSON at position 0 (but only sometimes)

That unexpected “u” is the first letter of the string “undefined”. It happens because your two asynchronous operations (i.e. loading the JSON and loading the window) are racing to completion, and if the JSON isn’t loaded fast enough, the window.onload method will attempt to parse the JSON string that isn’t loaded yet. A solution is to move … Read more

“document.getElementByClass is not a function”

You probably meant document.getElementsByClassName() (and then grabbing the first item off the resulting node list): You may still get the error document.getElementsByClassName is not a function in older browsers, though, in which case you can provide a fallback implementation if you need to support those older browsers.ShareImprove this answerFollow

Download File Using JavaScript/jQuery

Use an invisible <iframe>: To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file’s MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data. If you only want to open it in a … Read more