0

I know there are many questions about this issue but my problem is different I managed to get the data outside of $.getJSON() but I couldn't access them

 var country = {} ; 

     $.getJSON('https://freegeoip.net/json/', function (code) { 

        country.config = code ;

  });

console.log(country);

I will get these information

{}config: {ip: "**********", country_code: "LB", country_name: "Lebanon", region_code: "06", region_name: "Beirut", …}proto: Object

if I try to access them like this

country.config.country_code

I will get this error

fdgdfgd:643 Uncaught TypeError: Cannot read property 'country_code' of undefined

I can't even access this one

country.config

How can I get the data from this object ?

8
  • I changed the title I hope you read whole thing before mark it as duplicated. I managed to return the response I want to access it now Commented Jan 13, 2018 at 12:20
  • Your question is a 100% duplicate. Your code cannot work. You need to tackle and understand asynchronous calls. Your problem is not special or different. Commented Jan 13, 2018 at 12:23
  • But console.log printed the data outside getJSON function therefor it works fine, I got this solution from another answer and it's the simplest one. I just want to know how to access these data I can only print country I can't access further Commented Jan 13, 2018 at 12:27
  • No, read the other two duplicates as well. This. Can. Not. Work. Commented Jan 13, 2018 at 12:43
  • @deceze only the 1st linked duplicate is the same issue. The other two are just confusing things. The 2nd seems to be about a browser bug and the 3rd is an unusual aspect of console.log() interactivity. Commented Jan 13, 2018 at 14:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.