Question

I have the following code on my website to display weather info. It has worked fine until recently. It still works everywhere except when I am at work. It doesn't work on my work pc or my phone when at work, but it works everywhere else. It used to work when I was at work. We have a sonic wall at work, but my pc is exempt from it. I have other jquery on my site that is still working. Any ideas?

<script>
   jQuery(document).ready(function($) {
   jQuery.ajax({
   url :     "http://api.wunderground.com/api//conditions/forecast/q/autoip.json",
   dataType : "jsonp",
   success : function(parsed_json) { 
   var temp_f = parsed_json['current_observation']['temp_f'];
   var feelslike = parsed_json['current_observation']['feelslike_f'];
   var forecast = parsed_json['forecast']['txt_forecast']['date'];
   jQuery('#wg1').append(temp_f + " feels like " + feelslike);
     }
    });
   });
   setInterval(function refreshWeather() {
   jQuery(document).ready(function($) {
   jQuery.ajax({
   url : "http://api.wunderground.com/api//conditions/forecast/q/autoip.json",
   dataType : "jsonp",
   success : function(parsed_json) { 
   var temp_f = parsed_json['current_observation']['temp_f'];
   var feelslike = parsed_json['current_observation']['feelslike_f'];
   var forecast = parsed_json['forecast']['txt_forecast']['date'];
   jQuery('#wg1').html(temp_f + " feels like " + feelslike);
     }
    });
   });
   }, 900000);
</script>
Was it helpful?

Solution

From your comment, it sounds like parsed_json['current_observation'] is returning undefined, probably because parse_json isn't what your code expects it to be.

To troubleshoot, add a console.log(parsed_json) as the first line of your success() function and see what the output looks like. This way we can see what you're getting back from the server.

You can also try simply copying/pasting the URL into your browser window and looking at the JSON from there: http://api.wunderground.com/api//conditions/forecast/q/autoip.json

I'm guessing either 1) wunderground can't read your location and is sending back a JSON error, or some kind of JSON format that your code isn't expecting, or 2) your work is blocking the URL and you're getting nothing back or the HTML for an error page or something. You'll find out when you console.log the response and inspect it manually.

Edit: And if wunderground returns a consistent error format (check their API), you can make your code more robust by checking for that error in the response before trying to parse it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top