John
John

Reputation: 13729

new XMLHttpRequest().responseType empty

Why is responseType empty?

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]

No frameworks.

Upvotes: 3

Views: 2639

Answers (1)

John
John

Reputation: 13729

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:

Full Media Type/Mime

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

Specific Media Type/Mime

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

Upvotes: 5

Related Questions