lundi 22 janvier 2018

ember.js RestAdapter How to define ajaxoptions

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




Aucun commentaire:

Enregistrer un commentaire