Difference between responseText and response in ajax

3

Good! What is the difference between ResponseText and response of the XMLHttpRequest object? Thanks!

    
asked by user53467 22.07.2017 в 02:32
source

1 answer

2

According to the MDN :

XMLHttpRequest#responseText

  

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.

XMLHttpRequest#response

  

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.

What is the difference?

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 .

    
answered by 22.07.2017 в 02:53