10 examples of 'javascript read json file from url' in JavaScript

Every line of 'javascript read json file from url' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
93export function loadJSON(url: string, callback: Function): void {
94 if (!url.contains("://")) {
95 url = `${window.location.protocol}//${normalizeURL(window.location.host)}/${url}`;
96 }
97 metron.web.get(`${url}`, {}, null, "JSON", function (data: JSON) {
98 if (callback != null) {
99 callback(data);
100 }
101 });
102}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

103function loadJSON(url, callback) {
104 document.getElementById("loading").style.display = "flex";
105 var xobj = new XMLHttpRequest();
106 xobj.overrideMimeType("application/json");
107 xobj.open('GET', url, true);
108 xobj.onreadystatechange = function () {
109 if (xobj.readyState == 4 && xobj.status == "200") {
110 callback(xobj.responseText);
111 }
112 };
113 xobj.send(null);
114}
9function loadJSON (url, callback) {
10 const xhr = new XMLHttpRequest();
11 xhr.open('GET', url, true)
12 xhr.responseType = 'json'
13 xhr.onload = () => {
14 const status = xhr.status;
15 if (status === 200) {
16 callback(xhr.response, null)
17 } else {
18 callback(xhr.response, status)
19 }
20 }
21 xhr.send()
22}
4function loadJSON( url, callback ) {
5 var request = new XMLHttpRequest();
6 request.open('GET', url, true);
7
8 request.onload = function() {
9 if (request.status >= 200 && request.status < 400) {
10 callback( JSON.parse(request.responseText), "success" );
11 } else {
12 callback( false, "server error" );
13 }
14 };
15
16 request.onerror = function() {
17 callback( false, "connection error" );
18 };
19
20 request.send();
21}
29function loadJSON(url, callback) {
30 /*
31 Utils method to load a json on the server
32 @url: Url to the json file to load
33 @callback: Method to call when the json file is loaded
34 */
35 var xobj = new XMLHttpRequest();
36 xobj.overrideMimeType("application/json");
37 xobj.open('GET', url, true); // Replace 'my_data' with the path to your file
38 xobj.onreadystatechange = function () {
39 if (xobj.readyState == 4 && xobj.status == 200) {
40 // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
41 console.log(xobj.responseText);
42 callback(JSON.parse(xobj.responseText));
43 }
44 };
45 xobj.send(null);
46}
518function loadJSON(url, callback) {
519
520 var xobj = new XMLHttpRequest();
521 xobj.overrideMimeType("application/json");
522 xobj.open('GET', url, true);
523 xobj.onreadystatechange = function () {
524 if (xobj.readyState == 4 && xobj.status == "200") {
525 callback(xobj.responseText);
526 }
527 };
528 xobj.send(null);
529}
85function loadJSON(url, callback) {
86 var xobj = new XMLHttpRequest();
87 xobj.overrideMimeType("application/json");
88 xobj.open('GET', url, true); // Replace 'my_data' with the path to your file
89 xobj.onreadystatechange = function () {
90 if (xobj.readyState == 4 && xobj.status == "200") {
91 // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
92 callback(JSON.parse(xobj.responseText));
93 }
94 };
95 xobj.send(null);
96}
2function loadJSON (url, callback) {
3 var xobj = new XMLHttpRequest()
4 xobj.overrideMimeType('application/json')
5 xobj.open('GET', url, true)
6 xobj.onreadystatechange = function () {
7 if (xobj.readyState === 4 && xobj.status === 200) {
8 callback(xobj.responseText)
9 }
10 }
11 xobj.send(null)
12}
122function loadJson(url, callback) {
123 var XmlHttpRequest = new XMLHttpRequest();
124 XmlHttpRequest.overrideMimeType("application/json");
125 XmlHttpRequest.open("GET", url, true);
126 XmlHttpRequest.onreadystatechange = function () {
127 if (XmlHttpRequest.readyState == 4 && XmlHttpRequest.status == "200") {
128 callback(JSON.parse(XmlHttpRequest.responseText));
129 }
130 };
131 XmlHttpRequest.send(null);
132}
6export function loadJSON(url: string, callback: any) {
7 /*
8 Utils method to load a json on the server
9 @url: Url to the json file to load
10 @callback: Method to call when the json file is loaded
11 */
12 var xobj = new XMLHttpRequest();
13 xobj.overrideMimeType("application/json");
14 xobj.open('GET', url, true); // Replace 'my_data' with the path to your file
15 xobj.onreadystatechange = function () {
16 if (xobj.readyState == 4 && xobj.status == 200) {
17 // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
18 callback(JSON.parse(xobj.responseText));
19 }
20 };
21 xobj.send(null);
22}

Related snippets