1

my-code is:

    //load-regions
    $(function(){
        var tempSession={}, provincesURL, citiesURL, selectedProvinceID;
        provincesURL = 'http://localhost/wahed/data/regions/ksa/regions.json';
        citiesURL = 'http://localhost/wahed/data/regions/ksa/cities.json';
        $.getJSON(provincesURL, function(data){
            tempSession.provinces = data;

        });
        $.getJSON(citiesURL, function(data){
            tempSession.cities = data;;
        });
        console.log(tempSession);
        console.log(tempSession.provinces);
    });//load-regions

In my console: enter image description here

'tempSession' is returning both the array of objects result. but problem is when I access 'tempSession.provinces' or 'tempSession.cities' both are undefined.

What I need is to store the getJSON result into any variable so that I can access them from anywhere.

Amin
  • 681
  • 4
  • 9
  • 27
  • Simply store the value returned by the success callback into the variable. `var globalVariable = ""; $.getJSON("Path", function(data){ globalVariable = data; });` – Kunal Mukherjee Mar 01 '18 at 14:04
  • tried, but then 'globalVariable' returns 'undefined' – Amin Mar 01 '18 at 14:07
  • Then the success callback is not returning anything. – Kunal Mukherjee Mar 01 '18 at 14:07
  • 'tempSession' is returning both the array of objects result. but problem is when I access 'tempSession.provinces' or 'tempSession.cities' both are undefined. – Amin Mar 01 '18 at 14:08
  • Is this `citiesURL` link returning a JSON when we paste it in the browser? – Kunal Mukherjee Mar 01 '18 at 14:08
  • yes, it's a valid .json file, and returning json object – Amin Mar 01 '18 at 14:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166039/discussion-between-amin-and-kunal-mukherjee). – Amin Mar 01 '18 at 14:11
  • $.getJSON is used to fetch files from server and not on the client. Please check the path of `citiesURL`. Set it to `/wahed/data/regions/ksa/cities.json` and re-check. – Kunal Mukherjee Mar 01 '18 at 14:11

0 Answers0