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
Don't know why you don't just use the data
that you get back. 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.
<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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.