1

私はjQuery全体に比較的慣れていないので、これが本当に明白なことである場合はお詫び申し上げます。

私の基本的な問題は、JSON ファイルが有効であるにもかかわらず、.getJSON() 関数内の URL が何も返さないことです。

jQuery:

$(document).ready(function() {
  var key = '*****************'

  var getMusic = function() {
      $.getJSON('http://developer.echonest.com/api/v4/song/search?api_key=' + key + '&artist=led+zeppelin', function(data) {
          $('.songs').append('<li>success</li>');
          data['songs'].forEach(function(d) {
              $('.songs').append('<li>' + d['title'] + '</li>');
          });
      });
  };

  $('.click').click(getMusic);
});

したがって、「成功」はリストに追加されますが、他には何もありません。Chrome のデバッグ コンソールに次のエラーが表示されます。Uncaught TypeError: Cannot read property 'forEach' of undefined

これは、URL が関数に何も渡していないことを意味していると思います。ただし、URL は有効です。Chrome に入力するだけで、次の応答が得られます。

{  
  "response":{  
  "status":{  
     "version":"4.2",
     "code":0,
     "message":"Success"
  },
  "songs":[  
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOWOYIL14EEE38DB16",
        "artist_name":"Led Zeppelin",
        "title":"St. Tristan's Sword (Rough Mix)"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOWTZWK12A6D4FC7A8",
        "artist_name":"Led Zeppelin",
        "title":"Rock And Roll - 2007 Remastered Version Live Version From The Song Remains The Same"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOWYEOY14EEE39630C",
        "artist_name":"Led Zeppelin",
        "title":"Bring It On Home (Rough Mix)"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXAPFT12AF72A0776",
        "artist_name":"Led Zeppelin",
        "title":"The Ocean (Live Album Version)"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOTYJLS12A8C13B4A9",
        "artist_name":"Led Zeppelin",
        "title":"Somethin' Else - \"Tasty Pop Sundae\" Live Version From BBC Sessions"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOUQMIU12B0B8083DF",
        "artist_name":"Led Zeppelin",
        "title":"1-06 For Your Life"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOUKOUX12B0B80B0BA",
        "artist_name":"Led Zeppelin",
        "title":"308 - The Song Remains The Same"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXXFOR12A8C13A5C6",
        "artist_name":"Led Zeppelin",
        "title":"Whole Lotta Love - \"Top Gear\" Live Version From BBC Sessions"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXHHNN14DB525B914",
        "artist_name":"Led Zeppelin",
        "title":"Heartbreaker (For Voice)"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXWXGG12B0B80B0AF",
        "artist_name":"Led Zeppelin",
        "title":"1-07 Trampled Underfoot"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXWUUS12B0B80B0D9",
        "artist_name":"Led Zeppelin",
        "title":"412 - Moby Dick -- Bonzo's Montreux"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXVXFK12B0B80B0C2",
        "artist_name":"Led Zeppelin",
        "title":"02 Going to California"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXZGKC12A8C13B4C9",
        "artist_name":"Led Zeppelin",
        "title":"Heartbreaker - \"In Concert\" Live Version From BBC Sessions"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOXEDBF14EC9C340F9",
        "artist_name":"Led Zeppelin",
        "title":"10 Ribs & All/Carrot Pod Pod (Pod) (Reference Mix)"
     },
     {  
        "artist_id":"ARDIBRT1187B9AF176",
        "id":"SOYIZYO12C106D04DB",
        "artist_name":"Led Zeppelin",
        "title":"Ramble On (1999 Star System Mix)"
     }
    ]
   }
  }

コードが期待どおりに実行されない理由がわかりません。

4

2 に答える 2

2

$.getJSON You need to get response first:からのデータに間違ってアクセスしています。

data.response['曲']

$(document).ready(function() {
  var key = '*****************'

  var getMusic = function() {
      $.getJSON('http://developer.echonest.com/api/v4/song/search?api_key=' + key + '&artist=led+zeppelin', function(data) {
          $('.songs').append('<li>success</li>');
          data.response['songs'].forEach(function(d) {
              $('.songs').append('<li>' + d['title'] + '</li>');
          });
      });
  };

  $('.click').click(getMusic);
});
于 2015-12-14T18:18:41.497 に答える
0

がプロパティdataを持つルート JSON オブジェクトであることに注意してください。はプロパティ内にあるため、アクセスするにはアクセスする必要がありますresponsesongsresponsedata.response.songs

正しいコードは次のようになります。

data.response.songs.forEach(function (song) {
    $('.songs').append('<li>' + song.title + '</li>');
});

JSONP を使用する必要がある場合

@Pointy のコメントによると、セキュリティ上の理由から (おそらくクロスドメインの問題のため)、JSONPを使用する必要があるようです。

&callback=?JSONP を使用するには、サービス エンドポイントの末尾にコールバック関数を追加する必要があります。(jQuery はcallback、リクエストの送信時に適切な値を送信します)

// Note the trailing "callback=?"
$.getJSON('http://developer.echonest.com/api/v4/song/search?api_key=' + key + '&artist=led+zeppelin&callback=?')
.done(function (data) {
    $('.songs').append('<li>success</li>');
    data.response.songs.forEach(function (song) {
        $('.songs').append('<li>' + song.title + '</li>');
    });
});
于 2015-12-14T18:10:06.520 に答える