Search code examples
jqueryjsonajaxjsonpklout

Can't get klout score to show using ajax


I am trying to display a klout score on a web page

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

{"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 ??

Any help would be great


Solution

  • You have to remove that line of code after console.log, as it is not executed and the append do not work.

    <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)