Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用XMLHttpRequest获取网页,但仅获取html的一部分_Javascript - Fatal编程技术网

Javascript 使用XMLHttpRequest获取网页,但仅获取html的一部分

Javascript 使用XMLHttpRequest获取网页,但仅获取html的一部分,javascript,Javascript,我使用下面的代码获取网页(html) 但是它总是得到页面的一部分,而不是整个html代码 是否有任何参数设置返回html代码的长度 欢迎您发表评论将有多个readystatechange事件。只有当xhr.readyState===XMLHttpRequest.done==4时,请求才会完全完成 另外,htmlString=null。。。;htmlString=htmlString+xhr.responseText在很多方面都是假的。第一次,它将执行htmlString=null+“text”=

我使用下面的代码获取网页(html)

但是它总是得到页面的一部分,而不是整个html代码 是否有任何参数设置返回html代码的长度


欢迎您发表评论

将有多个
readystatechange
事件。只有当
xhr.readyState===XMLHttpRequest.done==4
时,请求才会完全完成

另外,
htmlString=null。。。;htmlString=htmlString+xhr.responseText在很多方面都是假的。第一次,它将执行
htmlString=null+“text”==“nulltext
。之后,它将一次又一次地添加
。responseText
(到目前为止检索到的内容),同时查看状态

另外,在相关说明中,您应该选中
xhr.status==200
,而不是
xhr.statusText==randomString
。如果是200,Web服务器不一定发送“OK”

读这本书

这样的方法应该更有效:

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.yahoo.com");
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 /* DONE */) {
     console.log(xhr.responseText.length);
     // Do something with it...
  }
}
xhr.send();

你能分享你收到的和你认为你应该收到的吗?
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.yahoo.com");
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 /* DONE */) {
     console.log(xhr.responseText.length);
     // Do something with it...
  }
}
xhr.send();