Facebook Plugin - Phonegap

Asked

Viewed 229 times

0

I’m using a Phonegap plugin to log in with Facebook, it’s working perfectly, I can get the email and user id, now, I needed to put this data in a JSON for me to work with it via ajax.

follows the code:

HTML

        <div class="event listening button" onclick="login();">Logar com Facebook</div>
        <div class="event listening button" onclick="apiTest();">Mostrar Dados</div>
        <div class="event listening button" onclick="logout();">Fazer Logout</div>

JS

        var login = function () {
          if (!window.cordova) {
             var appId = '305528339609022';
             facebookConnectPlugin.browserInit(appId);
          }
          facebookConnectPlugin.login( ["email"], 
             function (response) { alert(JSON.stringify(response)) },
             function (response) { alert(JSON.stringify(response)) });
        }

        var apiTest = function () { 
            facebookConnectPlugin.api( "me/?fields=id,email", ["user_birthday"],
                function (response) { alert(JSON.stringify(response)) },
                function (response) { alert(JSON.stringify(response)) }); 
        }

        var logout = function () { 
            facebookConnectPlugin.logout( 
                function (response) { alert(JSON.stringify(response)) },
                function (response) { alert(JSON.stringify(response)) });
        }

you can see that I do the login() then I click on the show data, there will appear an Alert for me showing the data. Now, I needed to put it in an array, to pick up via ajax...

you can see in the air

http://www.petfy.com.br/facebookconnect/www/

RIGHT NOW I GOT THE DATA, IT IS IN OBJECT

MY OBJECT

                var apiTest = function () { 
                facebookConnectPlugin.api( "me/?fields=id,email", ["user_birthday"],
                    function (response) { 
                      dados = response;
                    });
            }

the object is called "data", as I can take this object and make a json_enconde()?

  • put in an array to catch via ajax ? xDD

  • in apiTest(), the answer you receive is already a js Object, just store in a var qlqer

  • @What would you look like? var id = JSON.stringify(Answer); ?

  • you can declare var 1º at the top, where you want (var data), and then within the function you give the value (data = Sponse),,,,,, if you want an Object you don’t stringify xd, stringify is to transform jobject into a json string

  • I’ll put the code in an answer for you to see

  • @Enoqueduarte I understood, now, how to for example, to catch her via ajax?

  • what do you mean how to catch her via ajax ? the variable is set, just use it :)

  • uhm ? that code will hit the server ?? you are using Node.js ?

  • I think you’re confusing xd, javascript runs client-side

  • Yes @Enoqueduarte I meant that the code that connects with facebook will be on the server-side, for me to get this data on the server I have on client-side! understands?

Show 6 more comments

1 answer

0


Quite simple, defines a variable outside the call to api and inside the call guards to response in that variable... how you want an object, you don’t do the JSON.stringify() , stringify is only meant to represent the object in a json string (to display in an Alert like that you have for example)

   var dados;  

   var login = function () {
      if (!window.cordova) {
         var appId = '305528339609022';
         facebookConnectPlugin.browserInit(appId);
      }
      facebookConnectPlugin.login( ["email"], 
         function (response) { alert(JSON.stringify(response)) },
         function (response) { alert(JSON.stringify(response)) });
    }

    var apiTest = function () { 
        facebookConnectPlugin.api( "me/?fields=id,email", ["user_birthday"],
            function (response) { 
              dados = response;
              alert("sucesso !!!"+JSON.stringify(response));
            },
            function (response) { alert(JSON.stringify(response)) }); 
    }

    var logout = function () { 
        facebookConnectPlugin.logout( 
            function (response) { alert(JSON.stringify(response)) },
            function (response) { alert(JSON.stringify(response)) });
    }
  • Hello, welcome SO-PT, your answer seems to be good and answer the question, only that there is not much explanation around it, if possible, comment on your proposed solution so that we can better understand what it proposes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.