无法从JSON请求中获取数据,但我知道它已返回
我试图抓住从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); }); } }); }
我尝试了各种不同的代码,但我似乎无法打印这些值。
所有的帮助真的很感激!
您将dataType
指定为jsonp
但该服务只是返回json
,您不能使用crossdomain。
jQuery的错误消息是: "jQuery1710014922410249710083_1323288745545 was not called"
,这意味着回调未被调用,因为它应该是。
更新:
即使服务不支持JSONP格式,也有一种方法可以检索数据。 请参阅此链接了解详情。
我的例子是使用jquery.xdomainajax.js脚本,该脚本将ajax请求路由到YQL , YQL能够以JSONP格式检索整个HTML页面 。 所以下面的例子是使用普通的HTML页面来检索数据。
- 优点:
- 您可以检索任何HTML内容。
- 它非常灵活,因为您不依赖于Web服务可以获得的内容。
- 缺点:
- 它比较慢,因为您使用Yahoo服务来处理和获取整个HTML数据。
- 还传输了更多数据。
有关工作示例,请参阅此片段。
码:
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); } });
这只是一个示范。 如果您需要页面中的任何其他数据,请检查页面结构,检索并处理并显示在上面的示例中。
也许你受到跨域违规的打击? 您不能只从其他服务器托管的随机网页访问任何Web服务。
Songsterr不支持JSONP。 意思是,它不会使用给定的回调函数包装数据。