0

I'm trying to get the longitude and latitude I called in via ajax into their own variables so that I can use them in the request I'm going to make the the DarkSky API. I'm aware of them being local, and so can't be accessed outside of the ajax function, but I can't for the life of me work out a way around it. Here's my code:

$(document).ready(function() {

  //location API

  $("#myweather").load("window",function() {

    $("#location").fadeOut(function() {

      $.ajax({
        async: false,
        url: "https://geoip-db.com/jsonp",
        jsonpCallback: "callback",
        dataType: "jsonp",
        success: function(loc) {
          $("#location").html(loc.city)
          var long = loc.longitude
          var lat = loc.latitude
          }        
      });
    }).fadeIn();
  });      

      $("#result").load("location", function() {
        $("#result").fadeIn(function() {
          $.getJSON("https://api.darksky.net/forecast/15f8bf5641489ec32f66662221933c14/" + lat + long, function(forecast) {
            consolelog(forecast);
          });
        });
      });
});
  • Your AJAX `success` function should call whatever code it is that loads `result`. Basically you want to be saying "get this thing from geoip, then use that to load this result". – Niet the Dark Absol Jul 03 '17 at 15:08
  • why not turn the .load function into a $.fn.function and then call it in the success callback? – treyBake Jul 03 '17 at 15:09

4 Answers4

1

Try this:

$(document).ready(function() {

  //location API

  $("#myweather").load("window",function() {

    $("#location").fadeOut(function() {
      $.ajax({
        async: false,
        url: "https://geoip-db.com/jsonp",
        jsonpCallback: "callback",
        dataType: "jsonp",
        success: function(loc) {
          $("#location").html(loc.city)
          var long = loc.longitude
          var lat = loc.latitude

          $("#result").load("location", function() {
            $("#result").fadeIn(function() {
              $.getJSON("https://api.darksky.net/forecast/15f8bf5641489ec32f66662221933c14/" + lat + long, function(forecast) {
                consolelog(forecast);
              });
            });
          });
        }
      });
    }).fadeIn();
  });
});

If you declare lat and long outside of the ajax call, it will be available to the other function, but it may not be loaded when $.getJSON is called.

0

You should declare these two variables outside the function like shown below:

$(document).ready(function() {

  //location API
    var long = '';
    var lat = '';
  $("#myweather").load("window",function() {

    $("#location").fadeOut(function() {

      $.ajax({
        async: false,
        url: "https://geoip-db.com/jsonp",
        jsonpCallback: "callback",
        dataType: "jsonp",
        success: function(loc) {
          $("#location").html(loc.city)
          long = loc.longitude
          lat = loc.latitude
          }        
      });
    }).fadeIn();
  });      

      $("#result").load("location", function() {
        $("#result").fadeIn(function() {
          $.getJSON("https://api.darksky.net/forecast/15f8bf5641489ec32f66662221933c14/" + lat + long, function(forecast) {
            consolelog(forecast);
          });
        });
      });
});
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

Try:

$(document).ready(function() {
    var long, lat;

    //location API
    $("#myweather").load("window",function() {

        $("#location").fadeOut(function() {
           $.ajax({
               async: false,
               url: "https://geoip-db.com/jsonp",
               jsonpCallback: "callback",
               dataType: "jsonp",
               success: function(loc) {
                   $("#location").html(loc.city)
                   long = loc.longitude
                   lat = loc.latitude
               }        
          });
      }).fadeIn();
  });      

    $("#result").load("location", function() {
        $("#result").fadeIn(function() {
            $.getJSON("https://api.darksky.net/forecast/15f8bf5641489ec32f66662221933c14/" + lat + long, function(forecast) {
                consolelog(forecast);
            });
        });
    });
});

The variables will then be available in the whole scope of the document ready function

joshua miller
  • 1,686
  • 1
  • 13
  • 22
-1

Declare the 2 variables globally without the var

Ivan Notarstefano
  • 115
  • 1
  • 4
  • 18
  • this wouldn't be the best way of doing it - plus always answer with a full explanation as to why, so people learn instead of remember how to – treyBake Jul 03 '17 at 15:10