无法获得Klout主题来展示Ajax Json

时间:2017-04-18 20:42:50

标签: jquery json ajax api get

所以我设法得到klout分数显示现在我已经坚持使用klout主题它将出现未定义

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
  var settings = {

    "url": "http://api.klout.com/v2/user.json/29273435394300349/topics?key=7fn6tcf3zvptq9sw47aknmjr&callback=?",
    "method": "GET",
    "dataType": "json",
    "headers": {}
  }

  $.ajax(settings).done(function (data) {
    console.log(data);
    if(data){  //response is not null or undefined
        $("#topics").append('<li>' + data.displayName + '</li>');
    }else{
       alert('Empty response');
     }


  });
</script>

<h2>Klout Score</h2>
<ul id="topics"></ul>

json的调用是

[{"id":"8755541566022602326","displayName":"Cameras","name":"Cameras","slug":"cameras","imageUrl":"http://kcdn3.klout.com/static/images/cameras-1365719089171.jpg","displayType":"visible"},{"id":"10000000000000019045","displayName":"Street Photography","name":"Street Photography","slug":"street-photography","imageUrl":"http://kcdn3.klout.com/static/images/icons/generic-topic.png","displayType":"visible"},{"id":"5987004988760856409","displayName":"Canon","name":"Canon","slug":"canon","imageUrl":"http://kcdn3.klout.com/static/images/icons/generic-topic.png","displayType":"visible"},{"id":"1436","displayName":"Polaroid","name":"Polaroid","slug":"polaroid","imageUrl":"http://kcdn3.klout.com/static/images/icons/generic-topic.png","displayType":"visible"},{"id":"9219221220892053523","displayName":"Pablo Picasso","name":"Pablo Picasso","slug":"pablo-picasso","imageUrl":"http://kcdn3.klout.com/static/images/icons/generic-topic.png","displayType":"visible"}]

我想展示每个主题,如相机,街拍,佳能,宝丽来等

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

响应是一个数组。你必须循环它:

var settings = {
    "url": "http://api.klout.com/v2/user.json/29273435394300349/topics?key=7fn6tcf3zvptq9sw47aknmjr&callback=?",
    "method": "GET",
    "dataType": "jsonp"
}

$.ajax(settings).done(function (data) {
  for (var i = 0; i < data.length; i++) {
    $("#topics").append('<li>' + data[i].displayName + '</li>');
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Klout</title>
</head>
<body>
  <h2>Klout Score</h2>
  <ul id="topics"></ul>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</body>
</html>