简体   繁体   English

Ember获得POST请求的JSON响应

[英]Ember get the JSON response of a POST request

I have a Ruby On Rails API and a front end in ember apart. 我有一个Ruby On Rails API和一个分开的前端。 I'm trying to add a record from ember to my api. 我正在尝试从ember向我的api添加一条记录。 I can see in my api logs that my request is well executed and return a json. 我可以在api日志中看到我的请求执行得很好,并返回了json。 But in my ember app i try to print that JSON response and access to those informations.. 但是在我的余烬应用程序中,我尝试打印该JSON响应并访问这些信息。

here's the code from my controller to create a record from my ember app : 这是我的控制器中通过ember应用程序创建记录的代码:

export default Ember.Controller.extend({
  actions: {

    submit: function(){
      var result = this.store.createRecord("session", {email: this.get('email'), password: this.get('password')});
      var onSuccess =  function(post){

        console.log("kikou");
        console.log(post);
      };

      var onFail =  function(post){
        console.log("kikou");
        console.log(post);
      };

      result.save().then(onSuccess, onFail);
    }
  }

the JSON returned is : 返回的JSON是:

{"user":{"email":"gui@hotmail.com","authentication_token":"4vLAkiM1Ro5qN2HeNogM","id":1}}

How can I access to email, authentication_token etc...? 如何访问电子邮件,authentication_token等? Maybe I'm doing something wrong or it is not the proper way to create a record on my rails api? 也许我做错了什么,或者不是在Rails API上创建记录的正确方法?

I do not want to make any ajax code in my controller otherwise I do not see any interest to use Ember. 我不想在控制器中编写任何ajax代码,否则我看不到使用Ember的兴趣。

Thank you for your help, 谢谢您的帮助,

You should have a model user defined, as well as use the DS.ActiveModelAdapter for your Ember application: 您应该定义一个模型user ,并为您的Ember应用程序使用DS.ActiveModelAdapter

app/models/user.js app / models / user.js

import DS from 'ember-data';
export default DS.Model.extend({
  email: DS.attr('string'),
  authentication_token: DS.attr('string')
});

Then, on the onSuccess function you can access the data like this: 然后,在onSuccess函数上,您可以像这样访问数据:

var onSuccess =  function(post){
  console.log(post.get('id'));
  console.log(post.get('email'));
  console.log(post.get('authentication_token'));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM