You would need to declare the variable first and then assign the value in the success function as such:
var js;
$.getJSON(url,
function(data) {
js = data;
// or use your data here by calling yourFunction(data);
}
);
EDIT:
If your code looks something like the following, then it probably won't work:
var js;
$.getJSON(url,
function(data) {
js = data;
// or use your data here by calling yourFunction(data);
}
);
$("div").html(js);
That's because the $.getJSON() is an asynchronous function. That means the code underneath will continue executing without waiting for the $.getJSON to finish. Instead you would want to use the variable inside the success function as such:
var js;
$.getJSON(url,
function(data) {
// the code inside this function will be run,
// when the $.getJSON finishes retrieving the data
js = data;
$("div").html(js);
}
);
function(data){...}part is the callback of getJSON,datais the JSON response that you will get.$.getJSONin aJavaScriptvariable?