Results return UNDEFINED from a callback

  • 1
  • Problem
  • Updated 5 years ago
Hi there, I have set up a function to get the alert data and used a callback here.

$(function () {
// Specify the location and Api key
var apiKey = 'myapikey';
var location = 'zmw:00000.1.16172';

// Run the query (pull data from feed)
var url = 'http://api.wunderground.com/api/' + apiKey + '/alerts/q/' + location + '.json';

window['wCallback_3'] = function(data) {
// Get any weather alerts
var info = data.alerts;
// Warning level and color
$('#wWarning .wLevel').append('<TD>' + info.wtype_meteoalarm + '</TD>');
$('#wWarning .wColor').append('<TD>' + info.level_meteoalarm_name + '</TD>');

};

// Callback
$.ajax({
url: url,
dataType: 'jsonp',
contentType: "application/json",
cache: true,
jsonpCallback: 'wCallback_3'
});

});


I then add the following to display the data

<div id="wWarning">

<table class="wBox">
<h3>Data here</h3>
<h1 class="wLevel"></h1>
<h1 class="wColor"></h1>
</table>

</div>


However when I visit the page, the data is labelled as 'UNDEFINED'. I have used firebug to try and see where I am going wrong. I look for the callback and it seems to fetch all the data as seen below.

wCallback_3(
{
"response": {
"version": "0.1"
,"termsofService": "http://www.wunderground.com/weather/api/d/terms.html"
,"features": {
"alerts": 1
}
}
,
"alerts": [
{
"type": "WRN",
"wtype_meteoalarm": "3",
"wtype_meteoalarm_name": "Thunderstorms",
"level_meteoalarm": "2",
"level_meteoalarm_name": "Yellow",
"level_meteoalarm_description": "The weather is potentially dangerous. The weather phenomena that have been forecast are not unusual, but be attentive if you intend to practice activities exposed to meteorological risks. Keep informed about the expected meteorological conditions and do not take any avoidable risk.",
"description": "Rain",
"date": "2013-04-26 08:30:00 GMT",
"date_epoch": "NA",
"expires": "2013-04-26 20:00:00 GMT",
"expires_epoch": "NA",
"message": "Rain)",
"phenomena": "NA",
"significance": "NA",
"attribution": "Information provided by , <a href='http://meteoalarm.eu/'>EUMETNET - MeteoAlarm</a> Note: Time delays between this website and <a href='http://meteoalarm.eu/'>Meteoalarm.eu</a> are possible. For the most up-to-date information about alert levels as published by the participating National Meteorological Services, please visit <a href='http://meteoalarm.eu/'>Meteoalarm</a>. For terms of use of this information, and copyright information, see <a href='http://meteoalarm.eu/'>Meteoalarm</a> Terms of Use."
}
,
{
"type": "FLO",
"wtype_meteoalarm": "10",
"wtype_meteoalarm_name": "Rain",
"level_meteoalarm": "2",
"level_meteoalarm_name": "Yellow",
"level_meteoalarm_description": "The weather is potentially dangerous. The weather phenomena that have been forecast are not unusual, but be attentive if you intend to practice activities exposed to meteorological risks. Keep informed about the expected meteorological conditions and do not take any avoidable risk.",
"description": "Thunderstorms",
"date": "2013-04-26 08:00:00 GMT",
"date_epoch": "NA",
"expires": "2013-04-26 22:30:00 GMT",
"expires_epoch": "NA",
"message": "Thunderstorms)",
"phenomena": "NA",
"significance": "NA",
"attribution": "Information provided by , <a href='http://meteoalarm.eu/'>EUMETNET - MeteoAlarm</a> Note: Time delays between this website and <a href='http://meteoalarm.eu/'>Meteoalarm.eu</a> are possible. For the most up-to-date information about alert levels as published by the participating National Meteorological Services, please visit <a href='http://meteoalarm.eu/'>Meteoalarm</a>. For terms of use of this information, and copyright information, see <a href='http://meteoalarm.eu/'>Meteoalarm</a> Terms of Use."
}
]
}
);


Why is the data showing 'UNDEFINED'?
Photo of Ritchie Hamilton

Ritchie Hamilton

  • 2 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
because returned alerts struct is wrapped into an array. so

// Get any weather alerts
var info = data.alerts[0];

or cycle with a for
Photo of Ritchie Hamilton

Ritchie Hamilton

  • 2 Posts
  • 0 Reply Likes
thankyou