mardi 22 mars 2016

Ember store data to django backend

I'm new to Ember and I'm trying to communicate with a django server via RESTful API. So far I have managed to get the information from the server but if I try to send some information back I get an "Ember Data Request POST http://ift.tt/25jabBl returned a 500 Payload (text/html)" error.

I thought that the problem is that the url of the django api for the account is http://ift.tt/1WFN7GY (with a / at the end). So I thought that I should use the buildURL method that ember provides. Unfortunately, this method didn't work for me the way I used it.

So I' m in dead end...

Here is my code:

Server Response:

 [{"username":"user1","password":"123","email":"user1@example.com"},        
 {"username":"user2","password":"456","email":"user2@example.com"}]

Ember Model:

import DS from 'ember-data';

export default DS.Model.extend({
   username: DS.attr(),
   password: DS.attr(), 
   email: DS.attr()
});

Ember Adapter: import DS from 'ember-data';

   export default DS.RESTAdapter.extend({
    host: '/api',
    contentType: 'application/json',
    dataType: 'json',

  headers: {
    username: 'XXXX',
    password: 'XXXX'
}, 

buildURL: function(modelName, id, snapshot, requestType, query) {

    var url = this.buildURL(modelName, id, snapshot, requestType, query);

    return this._super(url) + "/";
}


});

Ember Serializer:

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
    primaryKey: 'username'
});

Ember Route: import Ember from 'ember';

export default Ember.Route.extend({
    model() {
       return this.store.findAll('account');
}
});

Ember Controller:

import Ember from 'ember';

export default Ember.Controller.extend({

    actions: {

   signup(){
    console.log('My username is: ', this.get('username'));
    console.log('My password is: ', this.get('password'));
    console.log('My email is: ', this.get('email'));

    var account = this.store.createRecord('account', {
            username: this.get('username'),
            password: this.get('password'),
            email: this.get('email')
    });

    account.save();

}
}
});

Thank you in advance for your help.




Aucun commentaire:

Enregistrer un commentaire