G
G
GaserV2017-05-11 20:00:42
Ember.js
GaserV, 2017-05-11 20:00:42

Why does Ember send data incorrectly when making a PUT request?

Such a disaster. I am sending a PUT request for an update. On the back for the test, I simply return the title. Through postman it works if you set the content type to application/x-www-form-urlencoded. I also installed it in the adapter, but the title returns in the postman, but not in the ember. Where could be the problem?

import RESTAdapter from 'ember-data/adapters/rest';
import config from '../config/environment';
import Inflector from 'ember-inflector';
import _ from 'lodash';

const { API_HOST, API_PREFIX } = config,
      inflector = Inflector.inflector,
      dataType = 'json',
      contentType = 'application/json; charset=utf-8';

export default RESTAdapter.extend({
  host: API_HOST,
  namespace: API_PREFIX,
  defaultSerializer: 'JSONSerializer',
  headers: {},

  sendRequest(body) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      Ember.$.ajax(body).then(
        data => Ember.run(null, resolve, data),
        jqXHR => {
          jqXHR.then = null; // tame jQuery's ill mannered promises
          Ember.run(null, reject, jqXHR);
        }
      );
    });
  },

  updateRecord(store, type, snapshot) {
    const pathname = inflector.pluralize(_.camelCase(type.modelName)),
          { id } = snapshot;

    let data = this.serialize(snapshot, { includeId: true }),
        namespace = this.get('namespace'),
        host = this.get('host'),
        sendRequest = this.get('sendRequest'),
        headers = this.get('headers'),
        dataType = 'text',
        contentType = 'application/x-www-form-urlencoded',
        url = `${host}/${namespace}/${pathname}/${id}`;

    let sendData = JSON.stringify(data);

    return sendRequest({ type: 'PUT', headers, url, dataType, contentType, sendData });
  },
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GaserV, 2017-05-12
@GaserV

I decided. The problem was that Ember sends json and Postman sends formdata. Accordingly, you need to return as $data = $request->json()->all(); $data['title']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question