Ajax导致div不起作用

时间:2013-06-13 10:15:16

标签: ajax

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

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/

2 个答案:

答案 0 :(得分: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"
},

答案 1 :(得分:-1)

尝试使用

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

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