I'm trying to save my returned JSON Array to variable in jQuery. I was looking for solution and I found this one:
var ajaxjson = [];
$.ajax({
async: "false",
dataType: "json",
url: "http://localhost:8080/mywebapp/getJSONFromCity",
data: {miasto: miasto},
success: function(result) {
ajaxjson = result;
}
});
also solutions from here: load json into variable, and here jQuery. Assign JSON as a result to a variable are not working for me:
var json = (function () {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': my_url,
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json;
})();
AJAX call is working fine, I can show my 'result' data inside my success call, but I can't assign that data to variable any way. After that AJAX call my var is still null, when I'm trying to show some object from that, e.g. if I try to log "json[0].id" I get error " Uncaught TypeError: Cannot read property 'id' of undefined"
Any ideas how to fix it?