Query just returns "undefined"--please help!

  • 1
  • Question
  • Updated 6 years ago
I wanted to dynamically create my query based on the city my user's in. This is basically a copy of the code sample for JQuery & JavaScript, except for the way the url is generated. When I run the code sample version, it works. When I run mine, I get "undefined" back.

Can anyone see an issue with this code? I've gone over it a ton and can't find anything wrong with it.

jQuery(document).ready(function ($) {

var urlQuery;
var urlBase;
var urlSubdivision;
var urlCity;

//Build the URL for the weather query.
switch (document.getElementById("MainContent_hdnCountry").value)
{
case 'Canada':
urlSubdivision = escape(document.getElementById("MainContent_hdnCountry").value + '/');
break;
case 'USA':
urlSubdivision = escape(document.getElementById("MainContent_hdnSubdivision").value + '/');
break;
}
urlBase = escape("http://api.wunderground.com/api/my_hidden_key/forecast10day/q/");
urlCity = escape(document.getElementById("MainContent_hdnCity").value + ".json");
urlQuery = (urlBase + urlSubdivision + urlCity);

$.ajax({
url: unescape(urlQuery),
dataType: "jsonp",
success: function (parsed_json) {
// v-- This line is returning "undefined".
alert(parsed_json['location']);
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_c'];
alert("Current temperature in " + location + " is: " + temp_c);
}
});
});
Photo of ingrediential

ingrediential

  • 2 Posts
  • 0 Reply Likes
  • frustrated

Posted 6 years ago

  • 1
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 123 Reply Likes
when you try hitting the URL on the browser, does it work there, but just not in your code? You can use "autoip" to pick up the user's location.

http://www.wunderground.com/weather/a...