mercredi 29 novembre 2017

How to send data as x-www-form-urlencoded in ember data

Out server api accepts data only in x-www-form-urlencoded form instead of json.

I can override the headers in model adapter:

import DS from 'ember-data';
export default DS.RESTAdapter.extend({
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
});

But I don't know how to serialize data now:

import DS from 'ember-data';
import { inject as service } from '@ember/service'
export default DS.JSONAPISerializer.extend({
  api: service('page-data'),
  serialize(snapshot, options) {
    var json = {
      stock_name: snapshot.attr('stock_name'),
      licence_type: snapshot.attr('licence_type'),
      priceable: snapshot.attr('priceable')
    };

    if (options.includeId) {
      json.stock_id = snapshot.stock_id;
    }
    // add csrf token
    json[this.get('api.pageData.csrf.token')] = this.get('api.pageData.csrf.hash')

    return json;
  }
});

This will always post data in json form to the api. But I need it to be in stock_name=*&licence_type=*&priceable=* form.




Aucun commentaire:

Enregistrer un commentaire