0

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?

Community
  • 1
  • 1
zeus
  • 51
  • 1
  • 5
  • can I see the server side script? the code tha you shown is OK. thanks – clement Jan 12 '15 at 13:59
  • @RequestMapping("/getJSONFromCity") public @ResponseBody List getJSONFromCity(@RequestParam(value = "miasto", required = true) String miasto) { return apartmentService.getApartmentsFromCity(miasto); } I'm using Spring with Hibernate – zeus Jan 12 '15 at 14:01
  • Are you sending valid json and valid headers? (mime type, control origin...) – jmingov Jan 12 '15 at 14:05
  • Yes, as I said, I get valid JSON from server, I can read that but only inside my success function, I only don't know how to assign my response data to other jQuery variable. – zeus Jan 12 '15 at 14:08
  • can we see th json returned to the client? Can you log the 'success' case to be sure that the line into success worked? – clement Jan 12 '15 at 14:11
  • It's too huge to paste it here, so here is link: http://paste.ofcode.org/WeizevTE9hcSdsUY8B9qjj Yes, I can log something inside my 'success' case and it's working good. I can also log my result inside 'success' and it's showing JSON properly. – zeus Jan 12 '15 at 14:21
  • You can't just make an AJAX call and expect the results to be there immediately. This is why your dynamic data is only available inside the `success` function. `success` is executed _after_ AJAX completes. You need a way to delay the rest of your script while the AJAX completes fetching the data. Look up promises and Q. – ninty9notout Jan 12 '15 at 14:34
  • Are you saying that in the second example when you "return json", json is still null? That is because the ajax call has not finished executing yet. You need to use your json variable in the success function of your ajax call. – user3589536 Jan 12 '15 at 14:38
  • I understand now, but how to load json at document.ready() and assign data to var use it later? – zeus Jan 12 '15 at 14:53
  • Even though you are making a synchronous call, which should work, I suggest to have a look at [How to return the response from an Ajax call?](http://stackoverflow.com/q/14220321/218196) – Felix Kling Jan 12 '15 at 16:23

1 Answers1

-1
try 
'success': function (data) {
json = $.parseJSON(data);
return json;
}
vik
  • 1
  • 2