0

I Have a script to where i get the city and state from a jquery get and when I ajax another url I get the console error "Uncaught ReferenceError: State is not defined"

Any Ideas?

jQuery(document).ready(function($) {
  $.get("http://ipinfo.io", function(response) {
    var City = response.city;
    var State = response.region;
  }, "json");

  var weather_url = "http://api.wunderground.com/api/mykey/geolookup/conditions/q/" + State + "/" + City + ".json"
  $.ajax({
    url: weather_url,
    dataType: "jsonp",
    success: function(parsed_json) {
      var location = parsed_json['location']['city'];
      var temp_f = parsed_json['current_observation']['temp_f'];
      alert(temp_f);
    }
  });
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
Barmar
  • 741,623
  • 53
  • 500
  • 612
T Varcor
  • 375
  • 1
  • 2
  • 11
  • you defined it inside the `get` - you need to move it outside, similar to `weather`or move `weather` to the same level as `state` – blurfus Jun 25 '15 at 19:38
  • 2
    You should call `$.ajax` inside the callback. Otherwise, you're doing the AJAX call before `$.get` completes. – Barmar Jun 25 '15 at 19:39
  • @T Varcor here is a nice page to read about Variable Scope in Javascript http://www.w3schools.com/js/js_scope.asp – Jacob Jun 25 '15 at 19:40

0 Answers0