10 examples of 'how to get json data from url in html' in JavaScript

Every line of 'how to get json data from url in html' 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
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}
15function get_data(url){
16
17 dir = url.split('/');
18 console.log("URL : "+ url);
19 var data = undefined;
20
21 for(var i in dir){
22 data = ds[dir[i]];
23 if(data == undefined) break;
24 }
25
26 return data;
27}
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}
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}
41function loadJson(url) {
42 var jsonData;
43 $.ajax({
44 type : "GET",
45 url : url,
46 async : false,
47 beforeSend : function(x) {
48 if (x && x.overrideMimeType) {
49 x.overrideMimeType("application/j-son;charset=UTF-8");
50 }
51 },
52 dataType : "json",
53 success : function(data) {
54 jsonData = data;
55 }
56 });
57
58 return jsonData;
59}
6function getJsonFromUrl(url) {
7 var query = url.split('?');
8 if (query.length < 2) {
9 // No queries so just return false
10 return false;
11 }
12 query = query[1];
13 // Collect REST params into a dictionary
14 var result = {};
15 query.split("&").forEach(function(part) {
16 var item = part.split("=");
17 result[item[0]] = decodeURIComponent(item[1]);
18 });
19 return result;
20}
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};
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}
677function getJson(url, callback) {
678 var xhr = new XMLHttpRequest();
679 xhr.open('GET', url, true);
680 xhr.withCredentials = true;
681 xhr.onreadystatechange = function() {
682 if (xhr.readyState === XMLHttpRequest.DONE) {
683 if (xhr.status >= 200 && xhr.status < 300) {
684 callback(null, xhr.responseText ? json.parse(xhr.responseText) : null);
685 } else {
686 callback(xhr.statusText || 'Unknown Error', null);
687 }
688 }
689 };
690 xhr.send();
691}
29function getJSON(url, callback) {
30 var xhr = new XMLHttpRequest();
31 xhr.open("GET", url, true);
32 xhr.onreadystatechange = function () {
33 if (xhr.readyState == 4) {
34 var response = JSON.parse(xhr
35 .responseText);
36 callback(response)
37 }
38 }
39 xhr.send();
40}

Related snippets