2017-01-28 4 views

答えて

1

最初にcontent.txtというファイルを作成し、表示するHTMLソースを記述します。例:
content.txt

Yep, this is the content.txt! 
Some html? 
<code>This is the code tag</code> 
<button class="btn btn-primary" onclick="hideAjax()">Hide content.txt</button> 
<h1>It works!</h1> 

HTML

<div id="ajaxResponse"> 
    content.txt isn't displayed 
</div> 
    <button type="button" onclick="loadAjax()" class="btn btn-primary">Show text from content.txt</button> 

JS/Ajaxの

function loadAjax() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     document.getElementById("ajaxResponse").innerHTML = 
     this.responseText; 
    } 
    }; 
    xhttp.open("GET", "content.txt", true); 
    xhttp.send(); 
} 

注:私はきたフンこれはtestingc.gaにありますので、バージョンを確認してください。

+0

** content.txt **のxhttp.openではhttps://domain.tld/directory/file.txtのような完全なパスを挿入できますが、クロスオリジン(CORSエラー)には注意してください。 !! –

関連する問題