Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does setting XMLHttpRequest responseType before calling open throw?

Tags:

Running new XMLHttpRequest().responseType = "json" in the console throws an "InvalidStateError" exception in Firefox 26 and IE11 but not in Chrome 31.

Why?

The Spec states that setting responseType

throws an "InvalidStateError" exception if the state is LOADING or DONE.

but in this case the state is UNSENT.

What's going on?

like image 337
Lex R Avatar asked Dec 24 '13 12:12

Lex R


People also ask

What is responseType?

The XMLHttpRequest property responseType is an enumerated string value specifying the type of data contained in the response. It also lets the author change the response type. If an empty string is set as the value of responseType , the default value of text is used.

Is XHR deprecated?

Synchronous XHR is now in deprecation state. The recommendation is that developers move away from the synchronous API and instead use asynchronous requests.


2 Answers

Note that setting responseType after I call open on the xhr object solved my problem (using IE11)

like image 67
Sebas Avatar answered Oct 11 '22 08:10

Sebas


It's a working draft, so it's normal if there are small differences or bugs in the implementations. I think Firefox still follows the behavior described in the draft of August 2011, where is specified that the exception is thrown if the state is not OPENED or HEADERS_RECEIVED.

like image 41
Carlo Cannas Avatar answered Oct 11 '22 10:10

Carlo Cannas