简体   繁体   English

无法获得使用ajax显示的klout分数

[英]Can't get klout score to show using ajax

I am trying to display a klout score on a web page 我正在尝试在网页上显示klout分数

this is my code 这是我的代码

       <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/********/score?key=7fn6tcf3zvptq9sw47aknmjr&callback=?",
    "method": "GET",
    "dataType": "json",
    "headers": {}
  }

  $.ajax(settings).done(function (data) {
    console.log(data);
    $(data, function( inf ) {
       $("#score").append('<li>' + inf.score + '</li>');
     });

  });
</script>

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

the json data that im calling is as follows 我调用的json数据如下

{"score":10.0,"scoreDelta":{"dayChange":0.0,"weekChange":0.0,"monthChange":0.0},"bucket":"10-19","unscored":true}

i cant get the klout score to show What am i doing wrong ?? 我无法获得klout分数来显示我在做什么错?

Any help would be great 任何帮助都会很棒

Don't know why you don't just use the data that you get back. 不知道为什么不只使用返回的data Try this instead. 试试这个吧。

 var settings = { "url": "http://api.klout.com/v2/user.json/233905743529873888/score?key=7fn6tcf3zvptq9sw47aknmjr&callback=?", "method": "GET", "dataType": "json", "headers": {} } $.ajax(settings).done(function (data) { console.log(data); $("#score").append( $('<li/>').text(parseInt(data.score, 10)) ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul id="score"></ul> 

You have to remove that line of code after console.log , as it is not executed and the append do not work. 您必须在console.log之后删除该行代码,因为它没有被执行并且append操作无效。

<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/233905743529873888/score?key=7fn6tcf3zvptq9sw47aknmjr&callback=?",
    "method": "GET",
    "dataType": "json",
    "headers": {}
  }

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


  });
</script>

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

It is safe approach to wrap your append into if-else block as it will be easy and relevant in determining response type (is null or not) 将您的append包装到if-else块中是一种安全的方法,因为它很容易确定相关的响应类型(是否为null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM