vendredi 8 avril 2016

response after ajax post request in ember

I am new to ember .I designed an ember app and integrate the fb login in it and have to send facebook accesstoken to app backend (post request) and fetch the token generated by backend(rails).

My Post request response is:

{
"token":"71fcb8c39dc6449e2ac8e88d21e4d008cf746e16a774aa8755f6be0dbc43849265f9010111986a912fde60de4f76eb5a600ec286b26ea0a865cc7f5cab49330a",
"user":{"role":"unverified"}
}

and the component is

    import Ember from 'ember';

export default Ember.Component.extend({
    fb: Ember.inject.service(),
    session: Ember.inject.service(),

    authenticate: function(accessToken) {
    console.log("accesstoken: "+accessToken);
      Ember.$.ajax({
        url: "http://localhost:3000/v1/sessions/fb_login",
                accepts: 'application/json',
                data: { token: accessToken},
                crossDomain: true,
                headers: {
                    'Client-Key': '403cb982d0c48473bee32b41b6765e15a26c595c689c620cece5fc15370c33c9c9f6d071f84bf6b88baf466f653f44b4524634bde6fbe68f065f06268f7ed7e2',
                },
        type: 'post',
        success:function(data){
          console.log('data is '+data);
        }
      });
  },

    actions: {

        fb_login() {
                this.get('fb').login().then((response) => {
                if (response.status === 'connected') {
                    let fbToken = response.authResponse.accessToken;
                    this.authenticate(fbToken);
                } else if( response.status === 'not_authorized') {
                    // code for not authrized
                    // this.transitionTo('/');
                } else {
                    // code for facebook login
                    // this.transitionTo('/');
                } 
            }).catch(() => {
                    // this.sendAction('check','/');
            });
        },
    }

});

But after ajax call success is never get called to I am unable to get the the response and browser always responds with:

pretender.js:132 XHR finished loading: POST "http://localhost:3000/v1/sessions/fb_login".

Somebody please explain me how the ajax work in ember for api calls.




Aucun commentaire:

Enregistrer un commentaire