Reputation: 193
I customized RESTAdapter to connect to a RestHeart server (a RestFull web gateway server for mongodb):
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host:'http://127.0.0.1:8080',
namespace: 'boards'
});
I created a model to test :
import DS from 'ember-data';
export default DS.Model.extend({
identity: DS.attr()
});
Everything works fine, but when I use the save method on a record (developer): I have a warning in the browser console :
The server returned an empty string for POST http://.../boards/Developer, which cannot be parsed into a valid JSON. Return either null or {}.
and the folowing error :
SyntaxError: Unexpected end of JSON input at parse () at ajaxConvert (jquery.js:8787) at done (jquery.js:9255) at XMLHttpRequest. (jquery.js:9548)
I know why : The RESTAdapter is waiting for a JSON response, and the restHeart server returns an empty response when adding => so jQuery causes an error when it tries to parse the null response.
With previous versions of ember-data, it was possible to set the dataType variable of jQuery ajax requests to '*' using the hook ajaxOptions this way:
export default DS.RESTAdapter.extend({
ajaxOptions(url, type, options) {
var hash = this._super(url, type, options);
hash.dataType = "*";
return hash;
},
host:'http://127.0.0.1:8080',
namespace: 'boards'
});
With ember-data 2.16, ajaxOptions is now private, and I do not know how to modify the dataType variable... so that the null response is not parsed as a JSON response
Versions :
Upvotes: 2
Views: 913
Reputation: 193
export default DS.RESTAdapter.extend({
ajaxOptions: function(url, type, options) {
// get the default RESTAdapter 'ajaxOptions'
var hash = this._super(url, type, options);
// override if it's a POST request
if (type == 'POST') {
hash.dataType = 'text';
}
return hash;
},
ajaxSuccess: function(jqXHR, data) {
if (typeof data === 'string') {
// return an empty object so the Serializer handles it correctly
return {};
} else {
return data;
}
},
host:'http://127.0.0.1:8080',
namespace: 'boards'
});
It works without any warning or error,strangely, because I don't know if I respect the encapsulation of the RESTAdapter class ...
see Ember-data 2.6 & 2.7 release notes
Upvotes: 2