Skip to content

Instantly share code, notes, and snippets.

@SpEcHiDe
Created June 16, 2016 16:34
Show Gist options
  • Save SpEcHiDe/4e0e32e938ab0ca32ad0a7f745bd9ded to your computer and use it in GitHub Desktop.
Save SpEcHiDe/4e0e32e938ab0ca32ad0a7f745bd9ded to your computer and use it in GitHub Desktop.
backing up hardware
var sendData = function(type, URL, formData, callBack){
// create a XHR object
var xhr = new XMLHttpRequest();
// open the XHR object in asynchronous mode
xhr.open(type, URL, true);
//xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=ISO-8859-1')
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// OK! we have a successful response.
var response = xhr.responseText;
//console.log('OUTPUT: ' + response);
// do something else with the response
callBack(URL, response);
}
};
// GET or POST the URL according to type
if(type == "GET"){
xhr.send();
}
if(type == "POST"){
xhr.send(formData);
}
};
var getBookDetails = function(isbn) {
// => https://ctrlq.org/code/20020-query-book-by-isbn
if(isbn.length == 10 || isbn.length == 13){
var url = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn;
sendData("GET", url, "", function(url, response) {
var results = JSON.parse(response);
document.getElementById('isbnOutput').innerHTML = "[API] <br> URL: " + url + "<br> Response: " + response;
if (results.totalItems == 1) {
// There'll be only 1 book per ISBN
var book = results.items[0];
var title = (book["volumeInfo"]["title"]);
var authors = (book["volumeInfo"]["authors"]);
var publisher = (book["volumeInfo"]["publisher"]);
var subtitle = (book["volumeInfo"]["subtitle"]);
var printType = (book["volumeInfo"]["printType"]);
var pageCount = (book["volumeInfo"]["pageCount"]);
var publishedDate = (book["volumeInfo"]["publishedDate"]);
var webReaderLink = (book["accessInfo"]["webReaderLink"]);
var remarks = "Published Date: " + publishedDate;
document.getElementById('bookname').value = title;
document.getElementById('bookauthor').value = authors;
document.getElementById('bookpublisher').value = publisher;
document.getElementById('bookremarks').value = remarks;
}
});
}
// => https://ctrlq.org/code/20020-query-book-by-isbn
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment