简体   繁体   English

将来自Ajax调用的响应存储在ember中

[英]Storing the response from ajax call in ember

I am new to ember and JavaScript.I have designed a ember app and integrate the fb login in it.Now I have to send the access_token of Fb login to app backend (post request) and fetch the new the token generated by backend(django).I also need to store the new token for future call to backend. 我是ember和JavaScript的新手,我已经设计了ember应用并将fb登录集成到其中,现在我必须将fb登录的access_token发送到应用后端(发布请求),并获取新的backend(django)生成的令牌)。我还需要存储新令牌,以供将来调用后端。

Post request response 发布请求回复

{
  "token": "b0fb466a6eac3ad87",
  "signup_done": true
}

controller 调节器

import Ember from 'ember'; 
/* global FB */

export default Ember.Controller.extend({ 
getLoginStatus: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    FB.getLoginStatus(function(response) {
      if (response.status) {
        resolve(response.status);
      } else {
        reject();
      }
    });
  });
}, 
getAccessToken: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    FB.login(function(response) {
      if (response.authResponse) {
        console.log("accesstoken>>>>>>>>>"+response.authResponse.accessToken);
        resolve(response.authResponse.accessToken);
      } else {
        reject(response);
      }
    }, { scope: 'email,user_photos,user_friends' });
  });
},

 authenticate: function(accessToken) {
    console.log("accesstoken<<<<<<<<<<<             "+accessToken);
      Ember.$.ajax({
        url: 'http://example.com/api/',
        data:{
          access_token: accessToken,
          provider:'facebook'
        },
        type: 'post',
        success:function(data){
          console.log('data>>>'+this.get(data));
        }
      });
  },

 testAPI:function() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
    });
},



   actions: {
    fblogin: function() {
      var self = this;
      this.getLoginStatus().then(function() {
        self.getAccessToken().then(function(accessToken) {
          self.authenticate(accesstoken)
            self.testAPI();
            console.log('success')
          }, function(reason) {
            console.log('failure', reason);
          });
        });

    },
    carousel:function (result) {
          var photos=result.photoThumbs
          this.set('model.photos',photos)
          console.log(this.get("model.results"));
          },
  }

});

I am able to make the ajax call to backend server and getting the response.But Not sure how to save the new token for future. 我可以对后端服务器进行ajax调用并获得响应。但是不确定如何保存新令牌以备将来使用。

If I understood your question correct then you can use localStorage or sessionStorage to save the Token for the future use. 如果我正确理解了您的问题,则可以使用localStorage或sessionStorage保存令牌以备将来使用。

$.ajax({
  url:url,
  data:data,
  success: function(res) {
    localStorage.token = res.token;
    sessionStorage.token = res.token;
  }
});

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

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