Javascript works in editor, fails in preview?

  • 1
  • Problem
  • Updated 3 years ago
  • Solved
I am testing some Javascript to pull National Weather Service alerts from http://www.wunderground.com/ and show it on the presentation window. It's very simple code, and it works in the layout editor:

http://imgur.com/hIdrWMs

However, when I save the presentation and then open the preview window, the code breaks and fails to work. If I press F12 in Google Chrome, it shows an error: "Uncaught SyntaxError: Unexpected end of input"  (program:1)

Is there some sort of bug in the Rise Vision preview window doing this? I am not an expert at Javascript, and so this code is hacked together, but the code works in the editor so I have no idea.

Here is the code, though note if you want to test this code yourself, you need to obtain a free developer "API key" to access their data. I have removed my API key from this example:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function($) {
	$.ajax({
		url : "http://api.wunderground.com/api/API-KEY-REMOVED/alerts/q/WI/Holcombe.json",
		dataType : "jsonp",
		success : function(parsed_json) {
			//	document.write('got here<br>');
			
			//ensure api gave good data
			if (parsed_json['response']['error']) {
				document.write('<b>API ERROR: </b>'+ parsed_json['response']['error']['description']);
				return;
			} else {
			//	document.write('no error detected.<p>');
			}
			
			//check if there are weather alerts
			if (parsed_json['alerts']['0']) {
				
				var i=0;
				parsed_json['alerts'].forEach(function() {
					document.write('<font color="'
						+ parsed_json['alerts'][i]['level_meteoalarm_name']
						+ '"><b>ALERT: '
						+ parsed_json['alerts'][i]['message']
						+ '</b></font>');
					i++;
				});
			}
			
			
		}
		});
	});
</script>

ATTRIBUTION: Portions of this code were pulled from here and customized:
http://www.wunderground.com/weather/api/d/docs?d=resources/code-samples
https://gist.github.com/jjfalling/8d18fba37cdd00ded05c
Photo of Dale Mahalko

Dale Mahalko

  • 170 Points 100 badge 2x thumb

Posted 3 years ago

  • 1
Photo of HSuarez

HSuarez

  • 11,896 Points 10k badge 2x thumb
Hi Dale,

Interesting project - looking at https://gist.github.com/jjfalling/8d18fba37cdd00ded05c it is using DHT22 data.  I've done a few projects with this sensor ( temperature/humidity sensor ) before (https://github.com/nethoncho/Arduino-DHT22).

It's good to have more than one pair of eyes to look at your work...Uncaught SyntaxError: Unexpected end of input"  (program:1) to me indicates that there is an error with your syntax.

I ran the code through this web app (http://jsbeautifier.org/ - it helps with making sense with the code ). I think there is a missing closing the function (ie } ) at the end, see attached screenshot.

I'm not sure if that works, if not, I hope that other people in the community can have a look at your work and the syntax (if not there are other places like Stack Overflow).

Thanks

(Edited)