Reputation: 1186
In my Ember v2.7.0 app, I need to use backend endpoint which does not return JSON payload. But it behaves like REST endpoint, so I thought I would just use DS.RESTAdapter
to fetch the data and convert the payload via DS.Serializer
.
Created this little Ember-twiddle, which just tries to fetch data with non-JSON payload. And it fails. As far as I can tell, it fails in DS.RESTAdapter
code, trying to extract JSON from the payload. So that my serializer does not have a chance to process the data.
This seems a bit odd, because I thought that Serializer is the layer which is responsible for munching the payload.
DS.RESTAdapter
for querying non-JSON endpoint?Upvotes: 0
Views: 155
Reputation: 17532
What you'll need to do here is creating your own adapter that derives from DS.RESTRAdapter
and then override its ajaxOptions-method. There you could change its dataType
to text
instead. I imagine that they separated this into its own method for your exact purpose since it doesn't do much else.
The Ember Guides have a page about customizing adapters that can get you started, based on the original code from the Ember repository it should probably be something like this.
import DS from 'ember-data';
import Ember from 'ember';
const {
get
} = Ember;
export default DS.RESTAdapter.extend({
ajaxOptions(url, type, options) {
var hash = options || {};
hash.url = url;
hash.type = type;
hash.dataType = 'text';
hash.context = this;
if (hash.data && type !== 'GET') {
hash.contentType = 'text/plain; charset=utf-8';
}
var headers = get(this, 'headers');
if (headers !== undefined) {
hash.beforeSend = function (xhr) {
Object.keys(headers).forEach((key) => xhr.setRequestHeader(key, headers[key]));
};
}
return hash;
}
});
Upvotes: 2