I'm new to JavaScript, and as an exercise, I'm trying to combine two snippets of code I found online.
I want to prompt the user for a keyword, call a list of books about the keyword from the Google books API, and print the list.
<html>
<head>
</head>
<body>
<div id="content"></div>
<p id="demo"></p>
<script>
function myFunction() {
var person = prompt("enter a keyword", "keyword");
if (person != null) {
var url="https://www.googleapis.com/books/v1/volumes?q=" + person;
document.write(url);
document.write(person);
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, false);
xhttp.send();
var obj = JSON.parse(xhttp.responseText);
document.getElementById("demo").innerHTML = obj.item.volumeInfo.title;
document.write("done")
}
}
</script>
<button onclick="myFunction()">Try it</button>
</body>
</html>
I added various document.write() statements to test how far the function gets. I can only get it past writing the url and keyword. It never gets to "done" at the end.
Both snippets of code worked before I combined them.
Anyone see what I'm doing wrong?