10 examples of 'javascript fetch json from url' in JavaScript

Every line of 'javascript fetch json 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
97function _fetchJson(url){
98 try{
99 var http = WinHTTP(url);
100 if(http.status==200){
101 return JSON.parse(http.responseText);
102 }else if(http.status==500){
103 Mpi.message = "Server error. message from server:'" + http.responseText + "'.";
104 }else{
105 Mpi.message = "Can not load package.server error-" + http.status + ".";
106 }
107 }catch(ex){
108 Mpi.message = ex.description;
109 }
110 return null;
111};
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

83function fetchJSON(url, callback) {
84 makeRequest(url, jsonHandler(callback));
85}
1export default function fetchJson(url, callback) {
2 const req = new XMLHttpRequest();
3 req.onload = () => {
4 if (req.status >= 200 && req.status < 300) {
5 const text = req.responseText;
6 try {
7 callback(null, JSON.parse(text));
8 } catch (e) {
9 callback(e, null);
10 }
11 } else {
12 callback(new Error(`${req.status}: ${req.statusText}`), null);
13 }
14 };
15 req.onerror = (e) => {
16 callback(e.error, null);
17 };
18 req.open('GET', url, true);
19 req.send(null);
20}
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}
1export function get_json(url, success=null, error=null) {
2 var request = new XMLHttpRequest();
3 request.open("GET", url, true);
4 request.onload = function() {
5 if (request.status >= 200 && request.status < 400) {
6 var data = JSON.parse(request.responseText);
7 if (success !== null) {
8 success(data);
9 }
10 } else {
11 if (error !== null) {
12 error(request);
13 } else {
14 console.log('onload : some error occured');
15 console.log(request);
16 }
17 }
18 };
19 request.onerror = function(){
20 if (error !== null) {
21 error(request);
22 } else {
23 console.log('onerror : some error occured');
24 console.log(request);
25 }
26 };
27 request.send();
28}
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}
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}
98async function loadJSON(url) {
99 const req = await fetch(url);
100 return req.json();
101}
1function getJsonData(url, callback){
2 var req = new XMLHttpRequest;
3 req.open("GET", url);
4 req.send();
5 req.onreadystatechange = function() {
6 var status = req.readyState;
7 if(status == 4){
8 loading.visible = false;
9 callback(req.responseText);
10 }
11 }
12}
112function get_json(url, callback) {
113 $.ajax({
114 url: url,
115 dataType: 'json',
116 success: function(data) {
117 callback(null, data);
118 },
119 error: function(err) {
120 callback('Error getting: ' + url);
121 }
122 });
123}

Related snippets