I am a beginner, trying to learn ajax
and working with json
files. I would like to use of JSON
formatted data. But the result of my request is an empty text. Update: Here is my piece of code:
var quoteContainer=document.getElementById("random-quote");
var btn=document.getElementById("btn");
btn.addEventListener("click",function(){
var myRequest=new XMLHttpRequest();
myRequest.open("GET","https://raw.githubusercontent.com/4skinSkywalker/Database-Quotes-JSON/master/quotes.json",true);
myRequest.addEventListener('load', function () {
var myData=JSON.parse(myRequest.responseText);
console.log(myRequest.responseText);
renderHTML(myData);
});
myRequest.send();
});
function renderHTML(data){
var LenJson=data.length;
var Num=Math.random()*LenJson;
console.log(Num);
var QuoteString="<p id='quote-text'>"+data[i].quoteText+"</p>"
var AuthorString="<p id='quote-author'>"+data[i].quoteAuthor+"</p>"
quoteContainer.insertAdjacentHTML('beforeend',QuoteString);
quoteContainer.insertAdjacentHTML('beforeend',AuthorString);
}
It still doesn't return any data. Why?