0

I'm fetching JSON data from a summarization API and I'd like to use one of the returned values as a variable I can later manipulate. I'm getting JSON data from this URL:

http://clipped.me/algorithm/clippedapi.php?url=http://www.bbc.com/news/world-asia-china-30067035

and it returns this:

{"title":"BBC News - Hong Kong protest leaders denied Beijing flight","summary":["They had hoped to meet China's leaders as part of their push for greater democracy, but were told at the airport that their travel permits were invalid.","They want Beijing to allow more candidates to stand in the territory's next leadership election in 2017.","The group were greeted at the airport by fellow democracy activists, who unfurled yellow umbrellas - a symbol of Hong Kong's democracy movement."],"source":"bbc.com"}

I've tried var story = json.results[0].summary; but it's not working.

My code looks like this:

$.ajax({
     type : "GET",
     crossOrigin: true,
     dataType : "jsonp",
     url : "http://clipped.me/algorithm/clippedapi.php?url=http://www.bbc.com/news/world-asia-china-30067035",
     success: function(data){
       var story = json.results[0].summary;
       $('p').html(story)
     }
});

Update: JSFIDDLE

0

3 Answers 3

1

Do you mean this?

   var story = data.summary[0];
   $('p').html(story)
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @scimonster I tried that but I got this error: "Uncaught SyntaxError: Unexpected token :"
In the browser console. Nothing is parsed into the page
Are you able to log story?
Unfortunately I can't. I've updated my question with a JSFIDDLE example
1

You result is it:

{
"title":"BBC News....",
"summary":[
    "They had...",
    "They want..",
    "The group..."
],
"source":"bbc.com"
}

this will work:

$.ajax({
 type : "GET",
 crossOrigin: true,
 dataType : "jsonp",
 url : "http://clipped.me/algorithm/clippedapi.php?url=http://www.bbc.com/news/world-asia-china-30067035",
 success: function(data){
   var story = data.summary[0];
   $('p').html(story)
 }

});

1 Comment

I get this error: "Uncaught SyntaxError: Unexpected token :" What does it mean?
1

Well the better function is:

$.getJSON("http://clipped.me/algorithm/clippedapi.php?url=http://www.bbc.com/news/world-asia-china-30067035&callback=?", function(data) {
    var story = data.summary[0];
    $('p').html(story)
});

Note: I add to end '&callback=?'

Then its work, but. Before you have that add in you server PHP. Thing like this

<?php
    echo $_GET['callback'] . '(' . "{'title' : 'value'}" . ')';
?>

Dont forget, callback, its for security of CrossDomain.

2 Comments

Hi Ray, I'm still getting this "Unexpected token :" error
It is not problem in javascript, its problem on your server, that should return format jsonp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.