I have a question regarding CORS that I hope someone can help me with. I am currently working on an assignment for freecodecamp.com where I need to create a Wikipedia article search based on user input. However, I am struggling to understand how adding headers would apply in my case since I don't have any. I have attempted to use jQuery, which works, but I want to achieve this using vanilla JavaScript for learning purposes.
UPDATE: Here is the JavaScript code I have been working on:
var url, searchBox, searchText;
url = 'https://en.wikipedia.org/w/api.php?action=opensearch&datatype=json&limit=10&search=';
searchBox = document.getElementById("searchBox");
var request = new XMLHttpRequest ();
function requestData ( e ) {
var str = e.target.value;
request.open("GET" , url + str );
request.onreadystatechange = function(){
if ( request.readyState === 4 ) {
if ( request.status === 200 ){
console.log(request.responseText);
}
else {
console.log("Server returned a status code of " + request.status);
}
}
};
request.send(null);
}
searchBox.addEventListener("keyup" , requestData);
In addition, here is the CSS code I have used for styling:
@import url('https://fonts.googleapis.com/css?family=Arimo:400,700');
/* CSS Reset */
body {
max-width: 960px;
width: 100%;
margin: 0 auto;
font-family: 'Arimo', sans-serif;
background: #000;
color: #fff;
}
h1 {
text-align: center;
font-size: 72px;
font-weight: 700;
}
/* More CSS styles included... */
If you can provide any insight or guidance on how to handle CORS and improve my code further, I would greatly appreciate it. Thank you!