私は初心者で、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);
}
それはまだすべてのデータを返しません。どうして?
は、あなたが使用してローカルファイルを、これをオフに実行しようとするすべての偶然です'file://' ...? –
@PatrickRoberts私は 'brackets'をテキストエディタとして使用しています。これは' chrome'ブラウザでコードを実行します。さて、私が言ったように、私は 'javascript'を学び始めました。 – Dalek
[Chromeのファイルドメインのクロスドメイン問題の予期しない複製]の可能な複製(https://stackoverflow.com/questions/6060786/file-url-cross-domain-issue-in-chrome-unexpected) –