我被困在鹅卵石中的json数据提取中。
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);
});
这是我得到的错误:
[INFO] ocess_manager.c:368: Heap Usage for 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
at pebble-js-app.js:123:32
at pebble-js-app.js:871:17
at req.onreadystatechange (lib/ajax.js:11
4:9)
答案 0 :(得分:3)
晚上莫娜,
删除网址末尾的问号。
删除放置它的card.show()
指令,并在添加副标题后放置。
现在你的最终代码应该是这样的:
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();
});
它现在应该完美运行。 :)
此外,您应该在ajax方法中添加一个失败回调:
ajax({object}, success, failure)