javascript - json data extraction in pebbleJS -
i stuck in json data extraction in pebble.
var ui = require('ui'); var ajax = require('ajax'); var url="http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=test&api_key=4a9f5581a9cdf20a699f540ac52a95c9&limit=10&format=json&callback=?"; var card = new ui.card({ title:'last.fm stat', subtitle:'fetching...' }); card.show(); ajax({ url: url }, function(data){ var topartist=data.topartists[0].artist.name; card.subtitle(topartist); });
here's error get:
[info] ocess_manager.c:368: heap usage app <lastfm sta: total size <48584b> used <6256b> still allocated <28b> [phone] pebble-app.js:?: (+) [card 1] : [card 1] [phone] pebble-app.js:?: javascript error: typeerror: cannot read property '0' of undefined @ pebble-js-app.js:123:32 @ pebble-js-app.js:871:17 @ req.onreadystatechange (lib/ajax.js:11
4:9)
evening mona,
remove question mark @ url's end.
remove
card.show()
instruction put it, , place after adding subtitle it.- specify you're dealing json datatype.
and final code should this:
var ui = require('ui'); var ajax = require('ajax'); var url="http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=test&api_key=4a9f5581a9cdf20a699f540ac52a95c9&limit=10&format=json&callback="; var card = new ui.card({ title:'last.fm stat', subtitle:'fetching...' }); ajax({ url: url, type: 'json' }, function(data) { var topartist = data.topartists.artist[0].name; card.subtitle(topartist); card.show(); });
it should run perfectly. :)
also, should add failure callback in ajax method:
ajax({object}, success, failure)
Comments
Post a Comment