Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var city;
  2.  
  3. var endpoint = 'http://ip-api.com/json/?fields=city,country,isp,status,message,countryCode';
  4.  
  5. var xhr = new XMLHttpRequest();
  6. xhr.onreadystatechange = function() {
  7.     if (this.readyState == 4 && this.status == 200) {
  8.         var response = JSON.parse(this.responseText);
  9.         if(response.status !== 'success') {
  10.             console.log('query failed: ' + response.message);
  11.             return
  12.         }
  13.         else{
  14.  
  15.         city = response.city;
  16.  
  17.         console.log("test1 " + city); //this is working
  18.  
  19.         }
  20.     }
  21. };
  22. xhr.open('GET', endpoint, true);
  23. xhr.send();
  24. console.log("test2 "+ city); //this is not working
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement