Reply To: JQuery Marine Weather

Forums Requests JQuery Marine Weather Reply To: JQuery Marine Weather

#20734
Alexander Bautz
Keymaster

Hi,
This URL throw this response when I try to get the file with ajax:

Failed to load https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘https://spjsblog.sharepoint.com’ is therefore not allowed access.

Do you control this datasoruce? – if you bring up the dev console and type in this (you need jQuery so you should run the code in the console from a DFFS enabled form):

jQuery.get("https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt", function( data ) {
    console.log(data)
});

Are you able to read it?

If you do, I can help you parse it like this:

var colDescr = [];
jQuery.get("/path to the file/latest_obs.txt", function( data ) {
    jQuery.each(data.split("\n"),function(i,line){
		if(i===1){
			colDescr = line.split(/ +/);
			colDescr[0] = "";
        }else{
			var cols = line.split(/ +/);
			console.log("Weather from station \""+ cols[0] + colDescr[0]+"\" winds from \""+ cols[8] + colDescr[8]+"\" at speed \""+ cols[9] + colDescr[9] +"\" Wave height \"" +cols[11] + colDescr[11] + "\"");
        }
    });		
});

This snippet outputs to the developer console like the attached image show.

Alexander

  • This reply was modified 5 years, 12 months ago by Alexander Bautz. Reason: Added image