I am working on some projects to access APIs. Eventually I may build a web app but for now for testing I am using HTML pages in Devonthink - aside from ease of archiving that seems to avoid XSS issues.
As an example this works fine in DT3 to use the Reddit API and retrieve a few posts:
<html dir="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body style="margin:1.5em; font-family:'Times-Roman','Times'; font-size:24px">
</body>
</html>
<script>
/* Use the Reddit API to retrieve a random Reddit post */
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.reddit.com/r/random.json', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = JSON.parse(xhr.responseText);
var post = data[0].data.children[0].data;
var postDiv = document.createElement('div');
postDiv.innerHTML = '<a href="' + post.url + '">' + post.title + '</a>';
document.body.appendChild(postDiv);
}
};
xhr.send();
/* accept a subreddit name and an integer and returns the top n posts from that subreddit */
var subreddit = 'javascript';
var n = 5;
var url = 'http://www.reddit.com/r/' + subreddit + '.json?limit=' + n;
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
var posts = data.data.children;
for (var i = 0; i < posts.length; i++) {
var post = posts[i].data;
var postDiv = document.createElement('div');
postDiv.innerHTML = '<a href="' + post.url + '">' + post.title + '</a>';
document.body.appendChild(postDiv);
}
} else {
console.log('error');
}
};
request.send();
</script>
I am trying to use essentially the same approach to retrieve PMIDs from Pubmed as part of a literature search. This code fails to give any response in DT3 - yet the URL itself gives a valid response.
Any idea why the Javascript does not work to retrieve data from the Pubmed API?
https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=low+back+pain&retmax=10
<html dir="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body style="margin:1.5em; font-family:'Times-Roman','Times'; font-size:24px">
</body>
</html>
<header>
Pubmed Test
</header>
<script>
/* Access the Pubmed Esearch utility to search for low back pain */
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=low+back+pain&retmax=10',true);
xhr.onload = function() {
if (xhr.status === 200) {
console.log('User\'s name is ' + xhr.responseText);
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
</script>