无法从JSON请求获取数据,虽然我知道它返回 [英] Cannot fetch data from JSON request although I know it's returned

查看:160
本文介绍了无法从JSON请求获取数据,虽然我知道它返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取从getJSON返回的数据,但我只是不能得到它的工作。我试过与search.twitter API相同的代码,它工作正常,但它不能与其他网站工作。我知道数据被返回,因为我可以找到它,当我使用Inspector。通过检查器找到的值是:

I'm trying to get a hold of the data returned from getJSON, but I just can't get it to work. I've tried the same code with the search.twitter API, and that works fine, but it doesn't work with the other site. I know that the data is returned cause I can find it when I use the Inspector. The values I find through the Inspector are:

[{"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);
            });
        }
    });
}



我尝试过各种不同的代码,但我不能

I've tried all sorts of different code, but I just can't seem to print the values.

推荐答案

您指定 dataType jsonp ,但服务只返回 json

You specified dataType as jsonp but the service is just returning json which you cannot use crossdomain.

jQuery的错误消息是:jQuery1710014922410249710083_1323288745545未被调用

jQuery's error message is: "jQuery1710014922410249710083_1323288745545 was not called" which means that the callback is not getting called as it should be.

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

There is a way how you can retrieve the data even when the service doesn't support JSONP format. See this link for details.

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

My example is using jquery.xdomainajax.js script which is routing the ajax request to YQL which is able to retrieve a whole HTML page in a JSONP format. So the example below is using normal HTML page to retrieve the data.


  • 优点:$ b​​ $ b

      <

    • 速度较慢,因为您使用Yahoo服务来处理和撷取整个HTML资料。

    • 还传送更多资料。

    有关工作示例,请参阅 代码段。

    See THIS snippet for working example.

    代码:

    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);
      }
    });
    

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

    This is just a demonstration. If you need any other data from the page then inspect structure of the page, retrieve it and process and shown in the example above.

    这篇关于无法从JSON请求获取数据,虽然我知道它返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆