Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AJAX responseType Handling. #2835

Closed
Parakleta opened this issue Sep 12, 2017 · 3 comments
Closed

AJAX responseType Handling. #2835

Parakleta opened this issue Sep 12, 2017 · 3 comments

Comments

@Parakleta
Copy link

I've been trying to retrieve an XML document via rx.Observable.ajax.get() and I've come across a few problems with the way the XHR.responseType field is handled.

The first is that the AJAX constructor sets a default .responseType of "json" if none is provided.

const request: AjaxRequest = {
async: true,
createXHR: function(this: AjaxRequest) {
return this.crossDomain ? getCORSRequest.call(this) : getXMLHttpRequest();
},
crossDomain: false,
withCredentials: false,
headers: {},
method: 'GET',
responseType: 'json',
timeout: 0
};

Note that there is already a getJSON() method which sets this value explicitly.

export function ajaxGetJSON<T>(url: string, headers?: Object): Observable<T> {
return mapResponse(
new AjaxObservable<AjaxResponse>({
method: 'GET',
url,
responseType: 'json',
headers
})
);
};

This is a problem because it means that non-JSON data cannot actually be retrieved via the default get() method. From the XHR response there are 3 ways to retrieve the body of the response: .response, .responseText, and .responseXML. If the .responseType is set then the body of the response must be successfully parsed by the associated parser or else the .response field is set to null. Note that .responseText is null unless the .responseType is unset or is set to "text". Similarly .responseXML is null unless the .responseType is unset or is set to "document".

The .responseType should be left unset as provides the most versatility, as then the .response, .responseText, and .responseXML fields may all be available depending on the source document.

The second is that a .responseType of "xml" is documented/used in the following code. The correct enumeration to request an XML response is "document".

/** @type {string} The responseType (e.g. 'json', 'arraybuffer', or 'xml') */
responseType: string;

case 'xml':
this.response = xhr.responseXML;
break;

If it is preferred to use "xml" as a more user-friendly value this shouldn't be exposed to the XHR object.
xhr.responseType = request.responseType;

@Parakleta
Copy link
Author

Just noticed that this also relates to #2007.

@dolezel
Copy link

dolezel commented Aug 2, 2019

Can someone look into it, please? It really prevents me from using ajax through rxjs.

@benlesh
Copy link
Member

benlesh commented Jul 14, 2021

This has been resolved in version 7

@benlesh benlesh closed this as completed Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants