怎样获取从服务器返回的数据体

Posted aisowe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样获取从服务器返回的数据体相关的知识,希望对你有一定的参考价值。

使用: xhr.response 属性, 可获得response body 内的数据, 注意, 这些数据类型不一样, 比如字符串 / 对象 / 二进制文件 等. 

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
  if (xhr.readyState === 4) {
    handler(xhr.response);
  }
}

 

注意: 

1. 如果返回的是xml或html, xhr.response会自动解析成网页文档, 而xhr.responseText 不会;

2. 如果xhr.responseType类型非空或为Text, 且xhr.readyState等于3, 则xhr.response 里面是存在从服务器返回的部分数据的;

以上是关于怎样获取从服务器返回的数据体的主要内容,如果未能解决你的问题,请参考以下文章