weixin_33738578 2016-10-13 03:13 采纳率: 0%
浏览 75

GET功能控制台错误

The logic:

my_function looks through file.txt, if it finds the word "Hello", returns true. If not, false.

The problem:

Uncaught type error: contents.includes is not a function

Limitations:

Can only use plain javascript

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();
}
  • 写回答

1条回答 默认 最新

  • Memor.の 2016-10-13 03:50
    关注

    use this.responseText instead of that parameter contents
    this.responseText is the response of your 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();
    }
    
    评论

报告相同问题?