我正试图抓住从getJSON返回的数据,但我无法让它工作。我已经尝试使用search.twitter API使用相同的代码,并且工作正常,但它不能与其他站点一起使用。我知道数据被返回,因为我可以在使用Inspector时找到它。我通过Inspector找到的值是:
[{"id":62093,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"It Will Follow The Rain"},{"id":62094,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"Pistol Dreams"},{"id":62095,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"Troubles Will Be Gone"},{"id":80523,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"Love Is All"},{"id":80524,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"I Won't Be Found"},{"id":80525,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"Where Do My Bluebird Fly"},{"id":80526,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"Sparrow And The Medicine"},{"id":80527,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"Into The Stream"},{"id":81068,"type":"Song","artist":{"id":12382,"type":"Artist","nameWithoutThePrefix":"Tallest Man On Earth","useThePrefix":true,"name":"The Tallest Man On Earth"},"title":"The Blizzards Never Seen The Desert Sands"}]
所以我知道他们是从服务器返回的。
我的js代码是:
function searchSongsterrForTab(){
var artist = "\"The Tallest Man On Earth\""
var url = "http://www.songsterr.com/a/ra/songs/byartists.json?callback=?&artists=" + artist;
$.ajax({
url: url,
dataType: 'jsonp',
success: function(data){
$.each(data, function(i, item){
console.log(item);
});
}
});
}
我尝试了各种不同的代码,但我似乎无法打印这些值。
真正感谢所有帮助!
答案 0 :(得分:1)
您已将dataType
指定为jsonp
,但该服务只是返回json
,您无法使用跨域。
jQuery的错误消息是:"jQuery1710014922410249710083_1323288745545 was not called"
这意味着回调没有按原样被调用。
即使服务不支持JSONP格式,也有一种方法可以检索数据。有关详细信息,请参阅this链接。
我的示例是使用 jquery.xdomainajax.js 脚本将ajax请求路由到YQL,该脚本能够在JSONP中检索整个HTML页面格式。所以下面的例子是使用普通的HTML页面来检索数据。
有关工作示例,请参阅 THIS 代码段。
<强>代码:强>
var artist = "The Tallest Man On Earth";
$.ajax({
url: 'http://www.songsterr.com/a/wa/search?pattern=' + escape(artist),
type: 'GET',
success: function(res) {
// see http://www.songsterr.com/a/wa/search?pattern=The%20Tallest%20Man%20On%20Earth
// 1) res.responseText => get HTML of the page
// 2) get odd anchors inside (it is zero-indexed) => get anchors containing song names
// 3) map array of anchor elements into only their text => get song names
var songs = $(res.responseText).find('div.song a:odd').map(function(i, el) { return $(el).text() });
console.log(songs);
}
});
这只是一个示范。如果您需要页面中的任何其他数据,请检查页面结构,检索并处理并显示在上面的示例中。
答案 1 :(得分:0)
也许您受到跨域违规的打击?您无法从其他服务器托管的随机网页访问任何网络服务。
答案 2 :(得分:0)
Songsterr不支持JSONP。意思是,它不会使用给定的回调函数包装数据。