new XMLHttpRequest().responseType 为空 [英] new XMLHttpRequest().responseType empty

查看:66
本文介绍了new XMLHttpRequest().responseType 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 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 旨在作为请求标头,并不代表媒体类型/来自服务器的 mime 响应(这在逻辑上是有意义的).因此,要测试响应类型,请使用以下内容:

As clarified in the comments the new XMLHttpRequest().responseType is intended as a request header and does not represent the media type/mime response from the server (which would have made logical sense). So to test for response types use something along the following lines:

完整媒体类型/Mime

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

特定媒体类型/Mime

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

这篇关于new XMLHttpRequest().responseType 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆