我有一个 JSONP,我想在列表中显示,我设法从列表中的文件中显示艺术家的姓名。但现在我想显示第三个 JSONP 对象,但我似乎可以获取它。我该怎么做?
我想显示"links": { "artists.albums": { href: "...", ...}
来自 JSONP 的。
JSONP 文件位于此处:http ://pcampbellem-test.nodejitsu.com/api/v1/artists.json?callback=MyCallback
JSONP(取自上面的 url,便于阅读):
MyCallBack({
"artists": [
{"id":"1","name":"Radiohead","website":"http://radiohead.com/","href":"/api/v1/artists/1.json"},
{"id":"2","name":"Nick Cave & The Bad Seeds","website":"http://www.nickcave.com/","href":"/api/v1/artists/2.json"},
{"id":"3","name":"John Frusciante","website":"http://johnfrusciante.com/","href":"/api/v1/artists/3.json"}
],
"meta": {
"artists": {
"page": 1,
"page_size": 10,
"count": 3,
"includes": [],
"page_count": 1,
"previous_page": null,
"next_page": null,
"previous_href": null,
"next_href": null
}
},
"links": {
"artists.albums": {
"href": "/api/v1/albums.json?artist_id={artists.id}", // I just wanna fetch this bit and display it in a list
"type": "albums"
}
}
});
JS:
(function($, window) {
var jsonUrl = 'http://pcampbellem-test.nodejitsu.com/api/v1/artists.json?callback=MyCallback'
$.ajax({
type: 'GET',
url: jsonUrl,
async: false,
jsonpCallback: 'MyCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
$.each(data, function(key, val) {
if (key === 'artists') {
for(var i = 0, max = val.length; i < max; i++) {
$('#artists-list').find('ul').append('<li><a href="' + val[i].website + '" class="edit">' + val[i].name + '</a></li>');
}
}
console.log(val.links); // this throws an error...
});
},
error: function(err) {
console.log(err.message);
}
});
}(jQuery, window));