0

I'm trying to get data from the open weather API but all I get is an object that I cannot use (the JSON is inside but I cannot access it)

I've read a lot about asynchronous JS and callbacks. I'm really not sure whether I need a callback for EACH API i'm using, I already used one to get latitude and longitude but now, for the open weather API, i need to pass these lat and lon as parameters for the API call, and I have no idea on how to do that if I use callbacks (as it seems that arguments in callbacks functions are not the ones used in the function but the ones that actually get returned, which I find extremely confusing).

Can anyone tell me what I'm doing wrong here?

$(document).ready(function() {
  $('#getWeather').on('click', function(){
    myLatAndLon(function(result) {
      var lat = result[0];
      var lon = result[1];
      console.log(myWeather(lat, lon));
      // Here, although the params work in browser, the message that gets returned in console is : "NS_ERROR_DOM_BAD_URI: Access to restricted URI denied"
    });
  })
})

function myLatAndLon(callback) {
  $.getJSON('http://ip-api.com/json/').done( function(location) {
    var arr = [];
    arr.push(location.lat);
    arr.push(location.lon);
    callback(arr);
  });
}

function myWeather(lat, lon) {
  return $.getJSON('http://api.openweathermap.org/data/2.5/weather', {
      lat: lat,
      lon: lon,
      APPID: 'a9c241803382387694efa243346ec4d7'
  })
  // The params are good, and when I type them on my browser, everything works fine
}

EDIT : I'm adding a picture to make it clearer what I'm trying to do and what I get eventually

El-Cortez
  • 113
  • 1
  • 11
  • 1
    Have you tried adding `http://` to the beginning of the url? – WillardSolutions Sep 15 '16 at 15:54
  • Hey, yes, sorry, I'm editing my post; The thing is, I get an object, but I cannot get the JSON, everytime I try to get it, it returns undefined... – El-Cortez Sep 15 '16 at 16:05
  • 1
    Have you parsed your JSON? `JSON.parse(returned_string);` – WillardSolutions Sep 15 '16 at 16:10
  • I just tried, and I get "SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data" – El-Cortez Sep 15 '16 at 16:14
  • Thanks a lot, I just added a picture to make it clearer – El-Cortez Sep 15 '16 at 16:17
  • 1
    I'm behind a corporate firewall so I can't see your picture. What's inside the object that gets returned? Does it contain your weather data? – WillardSolutions Sep 15 '16 at 16:19
  • It does, actually, it contains everything that gets returned by the server : - responseHeader, readyState, setRequestHeader, responseJSON, state, status, statusCode, etc. etc. I tried to directly print .responseJSON but it is undefined, I'm sure there a method somewhere to get it but I can't find it anywhere... – El-Cortez Sep 15 '16 at 16:25

1 Answers1

1

Change your code to it and test again :

$(document).ready(function() {
  $('#get').on('click', function() {
    myLatAndLon(function(result) {
      var lat = result[0];
      var lon = result[1];
      alert(JSON.stringify(result));
      $req = myWeather(lat, lon);
      $req.done(function(R) {
        alert(JSON.stringify(R))
      });
    });
  })
})

function myLatAndLon(callback) {
  $.getJSON('//ip-api.com/json/').done(function(location) {
    var arr = [];
    arr.push(location.lat);
    arr.push(location.lon);
    callback(arr);
  });
}

function myWeather(lat, lon) {
  return $.getJSON('//api.openweathermap.org/data/2.5/weather', {
    lat: lat,
    lon: lon,
    APPID: 'a9c241803382387694efa243346ec4d7'
  })
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="get">get</button>

And also you should check your server for CORS limitations read more here: http://enable-cors.org/server.html

MSS
  • 3,520
  • 24
  • 29
  • Hello, thanks a lot for your answer, I tried it, but unfortunately it doesn't change the result; in the end I get an object with a JSON inside it, but I still cannot access that JSON... – El-Cortez Sep 15 '16 at 16:09
  • wait i'am testing in fiddle – MSS Sep 15 '16 at 16:11
  • Thanks a lot, I just added a picture to make it clearer – El-Cortez Sep 15 '16 at 16:17
  • 1
    dude I check it again and again there is no problem I really sure you have `CORS` problem please change the setting in your server and test again and if you actually don't know what to do please mention your server info to help you. – MSS Sep 15 '16 at 16:37
  • 1
    see this post as well: http://stackoverflow.com/questions/6653825/error-access-to-restricted-uri-denied – MSS Sep 15 '16 at 16:39