xxxxxxxxxx
43
// Input from user
var input;
function setup() {
noCanvas();
// Grab the input and button from HTML
input = createInput('What is your question?');
var button = createButton('ASK AWAY');
// Attach a callback to button press
button.mousePressed(search);
}
// Run the API call
function search() {
var term = input.value();
// URL for querying the times
var url = 'https://api.nytimes.com/svc/topstories/v2/world.json?api-key=a65aff26acee41528286255c6b07c42b';
// Query the URL, set a callback
// 'jsonp' is needed for security
loadJSON(url, gotData, 'jsonp');
}
// Request is completed
function gotData(data) {
console.log(data);
// Go through and show some results
docs = data.response.docs;
// Iterate through the articles in "docs"
for (var i = 0; i < docs.length; i++) {
// Make each headline a link to the article
var headline = createElement('h3', '');
var link = createA(docs[i].web_url, docs[i].headline.main);
link.parent(headline);
// Make a <p> for "lead paragraph"
var par = createP(docs[i].lead_paragraph);
}
}