2016-10-13 3 views
0

ロジック:GET機能コンソールエラー

my_functionのそれは単語 "こんにちは" を見つけた場合、file.txtを経由見えtrueを返します。そうでない場合は、falseです。

問題:

キャッチされないタイプのエラー:contents.includes

制限機能ではありません。

function my_function() { 
var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function(contents) { 
if (this.readyState == 4 && this.status == 200) { 
//contents variable now contains the contents of the textfile as string 

//check if text file contains the word Hello 
var hasString = contents.includes("Hello"); 

//outputs true if contained, else false 
console.log(hasString); 

} 
}; 
xhttp.open("GET", "http://www.example.com/file.txt", true); 
xhttp.send(); 
} 
+0

@Tushar私が含まれて使用する場合は()の応答です。 – Malasorte

+0

'hasString = contents.indexOf(" Hello ")> -1;'あなたのブラウザで 'includes 'がサポートされているかどうかを確認してください。 – Tushar

+0

"内容"が空であるか未定義でないか確認する必要があります これを追加してください: var hasString; if(コンテンツ!==未定義&&コンテンツ!== ""){ hasString = contents.includes( "Hello"); } 戻ってくる結果が空でないことを確認してください。この状況では、状況を確認するだけでは十分ではありません。 –

答えて

1

はこれを使用するJavaScriptプレーンを使用することができます.responseTextそのパラメータの内容の代わりに
this.responseTextはjqueryのGET機能が働くと、あなたのAjax

function my_function() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
      //contents variable now contains the contents of the textfile as string 

      //check if text file contains the word Hello 
      var hasString = this.responseText.includes("Hello"); 

      //outputs true if contained, else false 
      console.log(hasString); 

     } 
    }; 
    xhttp.open("GET", "http://www.example.com/file.txt", true); 
    xhttp.send(); 
} 
+0

作品!ありがとうございました! – Malasorte

関連する問題