Global Javascript variable from AJAX JSON request Global Javascript variable from AJAX JSON request json json

Global Javascript variable from AJAX JSON request


The best way to approach this is by using what's called a callback function. A callback function is a function that is invoked when specific event takes place. In your case that event is the data being retrieved from your JSON endpoint (URL).

The proper way to do this is to create a function that will be called when your data is received and will then carry out the remaining logic. If you want to make that data also accessible globally, part of the callback function can update your global variable.

In the updated code below we first declare a global variable globalJSON that holds our data. Before you receive any data (i.e. before you click the button) the value of globalJSON.data will be null. Once the data is received the callback function updateView() is called with the received data. Inside of updateView() we update the global variable globalJSON.data and carry out the remaining logic (i.e. updating the required HTML elements).

You can then use globalJSON.data anywhere else in your code to get the data received when Update Details button was clicked.

// declare your global variable that will get updated once we receive datavar globalJSON = {    data: null}// this gets executed the moment you load the page - notice the value is nullconsole.log(globalJSON.data);// this gets executed AFTER you receive data - notice call to updateView() inside AJAX call functionfunction updateView(data) {    // this will update the value of our global variable        globalJSON.data = data;        // this is the rest of the logic that you want executed with the received data        document.getElementById("Name").innerHTML = data.name;    document.getElementById("Country").innerHTML = data.country;        // this will show that the global variable was in fact updated        console.log(globalJSON.data);}function loadJSON() {    var data_file = "https://www.tutorialspoint.com/json/data.json";    var http_request = new XMLHttpRequest();    try {        // Opera 8.0+, Firefox, Chrome, Safari        http_request = new XMLHttpRequest();    } catch (e) {        // Internet Explorer Browsers        try {            http_request = new ActiveXObject("Msxml2.XMLHTTP");        } catch (e) {            try {                http_request = new ActiveXObject("Microsoft.XMLHTTP");            } catch (e) {                // Something went wrong                alert("Your browser broke!");                return false;            }        }    }    http_request.onreadystatechange = function() {        if (http_request.readyState == 4) {            // Javascript function JSON.parse to parse JSON data            var jsonObj = JSON.parse(http_request.responseText);            updateView(jsonObj);            // jsonObj variable now contains the data structure and can            // be accessed as jsonObj.name and jsonObj.country.        }    }    http_request.open("GET", data_file, true);    http_request.send();}
 <h1>Cricketer Details</h1>		      <table class = "src">         <tr><th>Name</th><th>Country</th></tr>         <tr><td><div id = "Name">Sachin</div></td>         <td><div id = "Country">India</div></td></tr>      </table>      <div class = "central">         <button type = "button" onclick = "loadJSON()">Update Details </button>      </div>