微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 无法从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未被调用”,这意味着回调未被调用,因为它应该被调用.

更新:

即使服务不支持JSONP格式,也有一种方法可以检索数据.有关详细信息,请参见this链接

我的例子是使用jquery.xdomainajax.js脚本,它将ajax请求路由到YQL,它能够以JSONP格式检索整个HTML页面.所以下面的例子是使用普通的HTML页面来检索数据.

>优点:

>您可以检索任何HTML内容.
>它非常灵活,因为您不依赖于Web服务可以为您提供的服务.

>缺点:

>速度较慢,因为您使用Yahoo服务来处理和获取整个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);
  }
});

这只是一个示范.如果您需要页面中的任何其他数据,请检查页面结构,检索并处理并显示在上面的示例中.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐