new XMLHttpRequest().responseType 为空

Joh*_*ohn 3 javascript xmlhttprequest

为什么是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]
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

PHP

header('Content-Type: application/json');
//[Validated JSON Data]
Run Code Online (Sandbox Code Playgroud)

没有框架。

Joh*_*ohn 5

正如评论中所阐明的,new XMLHttpRequest().responseType它旨在作为请求标头,并不代表来自服务器的媒体类型/mime 响应(这在逻辑上是有意义的)。因此,要测试响应类型,请使用以下内容:

全媒体类型/Mime

console.log(xhr.getResponseHeader('content-type'));//application/json
Run Code Online (Sandbox Code Playgroud)

特定媒体类型/Mime

console.log(xhr.getResponseHeader('content-type').split('/')[1]);//json
Run Code Online (Sandbox Code Playgroud)