# Local weather app

**URL:** <https://forum.freecodecamp.org/t/local-weather-app/20520>\
**Category:** General\
**Created:** [July 15, 2016, 7:05pm UTC](https://forum.freecodecamp.org/t/local-weather-app/20520 "2016-07-15T19:05:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kfc5135](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kfc5135](https://forum.freecodecamp.org/u/kfc5135)\
**Post date:** [July 15, 2016, 7:05pm UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/1 "2016-07-15T19:05:57Z")

</div>

Hey, guys, I have just started my weather app, and I am already facing a problem.

$(document).ready(function(){  
//var long=0,lat=0;`var fetch="http://ip-api.com/json"; $.getJSON("http://ip-api.com/json", function (data) { var long=data.lon; var lat=data.lat; $("#data").html("lat="+lat+"<br>long="+long); }); // $("#data").html("lat="+lat+"<br>long="+long);<-- this is not able to display the longitude and latitude });`

The last line of my code (the one that I have commented) cannot display the longitude and latitude. I guess this is happening because once getJSON finishes all the local data is destroyed. Is there anyway i can pass those variables outside the function. Like we use pointers in C or c++ or return the value of the function call. I am really new JS.  
Please help me

---

<div class="post-metadata">

**Author:** ![kfc5135](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kfc5135](https://forum.freecodecamp.org/u/kfc5135)\
**Post date:** [July 16, 2016, 5:27am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/2 "2016-07-16T05:27:09Z")

</div>

I seriously need hep, please let me know if you guys are nit able to understand my question. Thanks

---

<div class="post-metadata">

**Author:** ![hubofthewest](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hubofthewest/32/70943_2.png) [@hubofthewest](https://forum.freecodecamp.org/u/hubofthewest)\
**Post date:** [July 16, 2016, 5:58am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/3 "2016-07-16T05:58:19Z")

</div>

Maybe an asynchronous problem.

Try putting your commented line inside the .getJSON function or make it a function and call it from there.

---

<div class="post-metadata">

**Author:** ![albert2309](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/albert2309/32/165607_2.png) [@albert2309](https://forum.freecodecamp.org/u/albert2309)\
**Post date:** [July 16, 2016, 6:03am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/4 "2016-07-16T06:03:33Z")

</div>

First of all, don’t panic.  
Have you checked the error in the console(press ctrl+shift+i) on your web browser?  
If the error, is

> VM1137 jquery-git.js:9465 Mixed Content: The page at ‘[https://jsfiddle.net/](https://jsfiddle.net/)’ was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint ‘[http://ip-api.com/json](http://ip-api.com/json)’. This request has been blocked; the content must be served over HTTPS.

or at least contain a phrase “Mixed Content”  
Then, you will have to find a service that uses secure HTTP in which the url begins with https://

Nonetheless, when I take the JSON data by my own, the latitude and longitude shows exactly what I wanted.

```js
var data = {"as":"AS4787 PT Cyberindo Aditama","city":"Jakarta","country":"Indonesia","countryCode":"ID","isp":"PT. Cyberindo Aditama","lat":-6.1744,"lon":106.8294,"org":"PT. Cyberindo Aditama","query":"101.128.76.53","region":"JK","regionName":"Jakarta","status":"success","timezone":"Asia/Jakarta","zip":""};
var long=data.lon;
var lat=data.lat;

console.log(long);
console.log(lat);
$("body").html("lat="+lat+"<br>long="+long);

```

---

<div class="post-metadata">

**Author:** ![Cardboard](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cardboard/32/68985_2.png) [@Cardboard](https://forum.freecodecamp.org/u/Cardboard)\
**Post date:** [July 16, 2016, 6:14am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/5 "2016-07-16T06:14:24Z")

</div>

Make sure your running this on a local server too. It wont work if your just opening the html file.

---

<div class="post-metadata">

**Author:** ![kfc5135](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kfc5135](https://forum.freecodecamp.org/u/kfc5135)\
**Post date:** [July 16, 2016, 7:55am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/6 "2016-07-16T07:55:12Z")

</div>

My code works and displays latitude and longitude but it can only to so inside getJSON, as soon as I want to use the variables in declared inside getJSON outside it, I am unable to get results.

---

<div class="post-metadata">

**Author:** ![0x0936](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/0x0936/32/10552_2.png) [@0x0936](https://forum.freecodecamp.org/u/0x0936)\
**Post date:** [July 16, 2016, 9:10am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/7 "2016-07-16T09:10:43Z")

</div>

`$.getJSON()` is an asynchronous operation - meaning, the rest of your code will continue to run until the callback is executed. The solution is to place your code which relies on the callback response, inside the callback.

```auto
function updateView(data) {
  // continue from here
}

$.getJSON(url, function(data) {
  updateView(data)
})

```

It’s possible to shorten this a bit when you’re comfortable with the idea that the callback function can be a named function which is passed the response object.

```auto
function updateView(data) {
  // continue from here
}

$.getJSON(url, updateView)

```

---

<div class="post-metadata">

**Author:** ![kfc5135](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kfc5135](https://forum.freecodecamp.org/u/kfc5135)\
**Post date:** [July 17, 2016, 5:07am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/8 "2016-07-17T05:07:16Z")

</div>

sweet, got it! Thank man owe you one 🙂

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 3:48am UTC](https://forum.freecodecamp.org/t/local-weather-app/20520/9 "2021-01-16T03:48:07Z")

</div>

Closed on January 16, 2021, 3:48am UTC


