Will this API work with just vanilla JavaScript (client-side requests)?
Categories
- 276 All Categories
- 4 LexiStats Corner
- 12 Review my code
- 6 Tutorials and presentations
- 89 Frequently asked questions
- 5 How to get useful technical help
- 2 Member guidelines
- 12 Suggest an improvement
- 56 Report a bug
- 7 Ask the Community: Other
- 52 Ask the Community: Technical and operational questions
- 57 General
Will this API work with just vanilla JavaScript (client-side requests)?
I was struggling to get it to work so I decided to check the forum. There are some posts that say it doesn't but they are from late 2018. Has it been changed?
Best Answer
-
TaisFukushima Member, Administrator, Moderator admin
Hello @avilches_terriza578
We accept requests from javascript code. But, we don’t accept CORS requests from javascript clients. They can use javascript to build an app and use our API, whereas, they can not use CORS request. My colleagues from the technical team verified this code in javascript and they ran it with nodejs, it worked for them (find the example below)
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var data = null;var xhr = new XMLHttpRequest();
// withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials.
xhr.withCredentials = true;xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});xhr.open("GET", "https://od-api.oxforddictionaries.com/api/v2/entries/en-gb/computer");
xhr.setRequestHeader("app_id", "XXXXXXXX");
xhr.setRequestHeader("app_key", "XXXXXXX);xhr.send(data);