JSFiddle - React, Tailwind, and code Playground
by adouga
JavaScript
var Data = {}; // would like to remove the global variable
function callBack() {
$.ajax({
url: "http://host/callBacks/script.js",
//get and execute Script to process json data below
dataType: "script"
});
}
$(document).ready(function() {
$.ajax({
type: "Get",
url: "http://host/data/json/",
success: function(json) {
Data = json; // Would like to just call the callback here
callBack(Data);
},
error: function() {
alert("error");
}
});
});
// Script which gets loaded from callBack
(function(Data) {
var json = $.parseJSON(Data);
$.each(json, function(i, v) {
alert(v);
});
})(Data);
// Desired code: Not working
var Data = {}; // Ideally would like to remove this from global scope if possible
$(document).ready(function() {
$.ajax({
type: "Get",
url: "http://host//data/json/",
success: function(Data) {
$.ajax({
url: "http://host/callBacks/script.js",
dataType: "script"
});
},
error: function() {
alert("error");
}
});
// Error: Length is null or not an object. In jQuery script