新的 XMLHttpRequest().responseType 为空

new XMLHttpRequest().responseType empty

为什么 responseType 是空的?

JavaScript

var xhr = new XMLHttpRequest();
xhr.open(method,url,true);
xhr.onreadystatechange = function()
{
 if (xhr.readyState=='4')
 {
  if (xhr.status==200)
  {
   console.log(xhr.responseType);//[empty]
  }
 }
}

PHP

header('Content-Type: application/json');
//[Validated JSON Data]

没有框架。

如评论中所述,new XMLHttpRequest().responseType 旨在作为请求 header,并不代表来自服务器的媒体 type/mime 响应(这在逻辑上是合理的)。因此,要测试响应类型,请使用以下几行内容:

全媒体Type/Mime

console.log(xhr.getResponseHeader('content-type'));//application/json

特定媒体 Type/Mime

console.log(xhr.getResponseHeader('content-type').split('/')[1]);//json