I'm unable to correctly return (or maybe "capture" is a better term) a JSON object from within a function that runs on page load. I'd like to capture that object so that I can subsequently POST it to another page by user action.
I'm running this on Windows 10, Firefox latest.
var configJson;
$(document).ready(function () { // Wait till page is loaded
configJson = getConfig();
console.log("Outside: " + JSON.stringify(configJson)); // DEBUG
});
function getConfig() {
var jqxhr = $.getJSON("/config/", function () {
//console.log("Success."); // DEBUG
})
.done(function (data) {
console.log("Inside: " + JSON.stringify(data)); // DEBUG
return data;
})
.fail(function () {
console.log("Fail."); // DEBUG
return JSON.parse("{}");
})
}
From the console, "Outside" is what is returned, "Inside" is what's seen in the function:
Inside: {"name":"fred"}
Outside: undefined