Good! What is the difference between ResponseText and response of the XMLHttpRequest object? Thanks!
Good! What is the difference between ResponseText and response of the XMLHttpRequest object? Thanks!
According to the MDN :
The XMLHttpRequest.responseText property returns to DOMString that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.
The XMLHttpRequest.response property returns the response's body. It can be of the type ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, depending on the value of XMLHttpRequest.responseType property.
The responseText
property will return always a DOMString, this is basically a normal string (UTF 16), containing the server response. You still specify in the answer the header Content-Type
, when using responseText
you will get the plain text representation of the answer.
On the other hand, the response
property gets the response according to the type that is expected and has been specified by means of XMLHttpRequest#responseType
. That is, if you specify that the answer will be a JSON:
xhr.responseType = 'json';
When you access the property response
you will get the response already parsed as a literal object. The same thing happens if you type a response type like arraybuffer to consume an image for example; when using the property response
you will get the buffer corresponding to the image.
The same applies for XMLHttpRequest#responseXML
that already returns the XML as a Document
.