我是初学者,试图学习ajax
并使用json
文件。我想使用JSON
格式的数据。但我的请求的结果是一个空的文本。 更新:这里是我的一段代码:尝试从git存储库中提取json格式的数据
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);
}
它仍然不返回任何数据。为什么?
任何机会尝试使用运行此掀起了本地文件你'文件://'...? –
@PatrickRoberts我使用'brackets'作为文本编辑器,它在'chrome'浏览器中运行代码。那么,正如我所说我刚开始学习'javascript',我可能会做错事。 – Dalek
[Chrome浏览器出现意外存在的文件URL跨域问题]的可能重复(https://stackoverflow.com/questions/6060786/file-url-cross-domain-issue-in-chrome-unexpected) –