javascript - 无法从 JSON 请求中获取数据,尽管我知道它已返回

标签 javascript jquery json cross-domain

我试图获取从 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,您不能使用跨域。

jQuery 的错误消息是:"jQuery1710014922410249710083_1323288745545 was not called" 这意味着回调没有被调用,因为它应该被调用。

更新:

即使服务不支持 JSONP 格式,也有一种方法可以检索数据。参见 this详情链接。

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

  • 优点:
    • 您可以检索任何 HTML 内容。
    • 它非常灵活,因为您不依赖网络服务可以为您提供什么。
  • 缺点:
    • 速度较慢,因为您正在使用 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);
  }
});

这只是一个演示。如果您需要页面中的任何其他数据,请检查页面的结构,检索它并进行处理,如上例所示。

关于javascript - 无法从 JSON 请求中获取数据,尽管我知道它已返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8421801/

相关文章:

javascript - 对输入元素的每次更改运行函数 : JQuery

javascript - 在 AngularJS 中使用 ng-style 和百分比值设置 HTML 元素宽度

php - jQuery JSON 下拉不填充

java - JSON 的 Java 正则表达式无效

javascript - 使用 jquery 删除未使用的 li 类

javascript - 给定单词列表一次并按顺序将单词包装在带有强标签的字符串中

javascript - 对象解构分配的任何方面都是通过引用进行的吗?

javascript - 如何扩展div以覆盖剩余页面高度

javascript - 如何让函数调用先执行然后再执行后续代码?

javascript - JSON.parse() 导致错误 : `SyntaxError: Unexpected token in JSON at position 0`