0

我正在尝试将此查询的结果放在单独的 div 中,但我有错误“无法读取未定义的属性搜索”......我找不到原因......请问有什么想法吗?

jQuery:

$("#searchterm").keyup(function(e){
        var q = $("#searchterm").val();
        $.getJSON("http://api.soundcloud.com/tracks.json?callback=?",
        {
          srsearch: q,
          client_id: "4346c8125f4f5c40ad666bacd8e96498",
          limit: "50"
        },
        function(data) {
          $("#results").empty();
          $("#results").append("<p>Results for <b>" + q + "</b></p>");
          $.each(data.query.search, function(i,item){
            $("#results").append("<div>" + item.title + item.user_id + "<br></div>");
          });
        });
      });

html:

<h1>Search</h1>
    <br />
    <input id="searchterm" />
    <button id="search">search</button>
    <div id="results"></div>

http://jsfiddle.net/TzQJP/2/

4

2 回答 2

0

更改$.each(data.query.search, function(i,item)$.each(data, function(i,item)

您的响应json对象不包含字段query.search

http://jsfiddle.net/TzQJP/3/

我得到的响应对象:

{
    "kind": "track",
    "id": 96698718,
    "created_at": "2013/06/13 10:37:13 +0000",
    "user_id": 41342328,
    "duration": 229010,
    "commentable": true,
    "state": "finished",
    "original_content_size": 9360679,
    "sharing": "public",
    "tag_list": "afrobeats charmytuch lagos",
    "permalink": "charmy-tuch-nabania-charmytuch",
    "streamable": true,
    "embeddable_by": "all",
    "downloadable": false,
    "purchase_url": null,
    "label_id": null,
    "purchase_title": null,
    "genre": "Afrobeats",
    "title": "Charmy Tuch - Nabania @charmytuch",
    "description": "Charmy Tuch - Nabania @charmytuch lagos artist",
    "label_name": "",
    "release": "",
    "track_type": "",
    "key_signature": "",
    "isrc": "",
    "video_url": null,
    "bpm": null,
    "release_year": null,
    "release_month": null,
    "release_day": null,
    "original_format": "mp3",
    "license": "all-rights-reserved",
    "uri": "http://api.soundcloud.com/tracks/96698718",
    "user": {
        "id": 41342328,
        "kind": "user",
        "permalink": "afrobeat360",
        "username": "afrobeat360",
        "uri": "http://api.soundcloud.com/users/41342328",
        "permalink_url": "http://soundcloud.com/afrobeat360",
        "avatar_url": "http://i1.sndcdn.com/avatars-000038616553-u1tot1-large.jpg?a210ade"
    },
    "permalink_url": "http://soundcloud.com/afrobeat360/charmy-tuch-nabania-charmytuch",
    "artwork_url": "http://i1.sndcdn.com/artworks-000050485407-jojf9f-large.jpg?a210ade",
    "waveform_url": "http://w1.sndcdn.com/yab7Zb0GaAdh_m.png",
    "stream_url": "http://api.soundcloud.com/tracks/96698718/stream",
    "playback_count": 0,
    "download_count": 0,
    "favoritings_count": 0,
    "comment_count": 0,
    "attachments_uri": "http://api.soundcloud.com/tracks/96698718/attachments"
},
于 2013-06-13T10:48:15.747 回答
-1

尝试使用

var dataJSON = eval('(' + data + ')');

有时发送的数据不是特别是 json 格式。eval 函数将其转换为 json。

于 2013-06-13T10:47:49.670 回答