2017-10-06 10 views
-2
<div id="new"></div> 
<button id="btn" onclick="changeContent('C:\Users\MarshMellow\Desktop\whatever.txt', newFunc)">Click Here!!</button> 
<script type="text/javascript"> 
function changeContent(url,callFunc) { 
    var xhttp;`declaration' 
    xhttp.onreadystatechange= function() { 
     if(this.readyState == 4 && this.status == 200) { 
      callFunc(this); 
     } 
    }; 
    xhttp.open("GET","C:\Users\MarshMellow\Desktop\whatever.txt",true); 
    xhttp.send(); 
} 
function newFunc(xhttp) {`Function call` 
    document.getElementById("new").innerHTML=xhttp.responseText; 
} 

まだ出力されていません。 AJAXコールが正常に動作していないようです。どのようにAJAX呼び出しを使用して任意のテキストまたはXMLファイルにアクセスできますか?

+1

を、このファイルを使用して私のために動作するコードの代わりに、コードの画像を投稿してください。また、コンソールにエラーが表示されますか? – tommyO

+0

また、あなたの画像から 'callFunc'が呼ばれていますが、定義されていないようです。 – tommyO

+0

あなたのブラウザはおそらくXHR経由でローカルファイルを読み込むことを許可しません。 –

答えて

0

コンピュータ上の静的ファイルを使用しているように見える場合は、ajax呼び出しを行うかどうかをリクエストするサーバーが必要です。 Ajaxコールを使いたい場合は、サーバーを設定してサイトのディレクトリに配置してください。 別のオプションはファイル入力を使用しています。filereader api

-1

XMLHttpRequestオブジェクトをインスタンス化する必要があります。

それは https://www.w3schools.com/xml/note.xml

function changeContent(url, callFunc) { 
    var xhttp = new XMLHttpRequest(); // instantiate XMLHttpRequest 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     callFunc(this); 
    } 
    }; 
    xhttp.open("GET", url, true); 
    xhttp.send(); 
} 

基本ドキュメントThe XMLHttpRequest Object

関連する問題