obtener respuesta de FB Api JavaScript

i try to get a function return in combination with facebook api reqeust.

var g_ret = true;

function uploadImagesFbCounter(anz){
    var gid='';
    $("div[id ^= 'gallerydetail']").each(function (i) {
        gid = this.id.split('-');
        gid = parseInt(gid[1]);
    })
    if(gid==0) return true;

    FB.api('/me', function(response) {

        //console.log(response);
        var uid = response.id;

        if(!anz){
            g_ret = $.ajax({
                type: "POST",
                async:false,
                url: "api/gallerie_anz_speich.php",
                data: "uid="+uid+"&op=get&gid="+gid
            });

            if(g_ret.response >= 20)    {
                g_ret = false;
            }
        }   else    {
            g_ret = $.ajax({
                type: "POST",
                async:false,
                url: "api/gallerie_anz_speich.php",
                data: "uid="+uid+"&op=set&gid="+gid
            });
            //console.log(g_ret.response);
            g_ret = '<span style="padding:0 5px;">Noch '+(20-g_ret.response)+'Fotos</span>';
            console.log(g_ret);
        }
    });

    return g_ret;
}

what ever I do, i get an empty response.... please help!

preguntado el 09 de marzo de 12 a las 16:03

2 Respuestas

The API you're using is asincrónico. You can't return a value from your function like that; it's just impossible in such a situation.

Instead, write your API so that its clients pass it a callable function. Inside the Facebook API callback, you can call that function and pass it that "g_ret" string.

function uploadImagesFbCounter(anz, callback){
  // ...
  FB.api('/me', function(response) {
    // ...
    callback(g_ret);
  });
}

Then when you call your function, instead of:

var result = uploadImagesFbCounter( whatever );
// ... do something with result ...

harías esto:

uploadImagesFbCounter( whatever, function( result ) {
  // ... do something with result ...
});

respondido 09 mar '12, 16:03

I just updated the answer with the basic idea. I'll add a little more. - Puntiagudo

Getting the user information from Facebook's servers is asynchronous. This means, any code you write after FB.api() does not wait for Facebook to respond. Your browser is not blocked just to wait for Facebook's servers. This is the same concept as AJAX, which I believe you are familiar with because I see you use it in your code.

The common way to "get a return value" from an asynchronous server request is to use a callback function.

function uploadImagesFbCounter(anz, onSuccess){
    var gid='';
    $("div[id ^= 'gallerydetail']").each(function (i) {
        gid = this.id.split('-');
        gid = parseInt(gid[1]);
    })
    if(gid==0) return true;

    FB.api('/me', function(response) {

        //console.log(response);
        var uid = response.id;

        if(!anz){
            g_ret = $.ajax({
                type: "POST",
                async:false,
                url: "api/gallerie_anz_speich.php",
                data: "uid="+uid+"&op=get&gid="+gid
            });

            if(g_ret.response >= 20)    {
                g_ret = false;
            }
        }   else    {
            g_ret = $.ajax({
                type: "POST",
                async:false,
                url: "api/gallerie_anz_speich.php",
                data: "uid="+uid+"&op=set&gid="+gid
            });
            //console.log(g_ret.response);
            g_ret = '<span style="padding:0 5px;">Noch '+(20-g_ret.response)+'Fotos</span>';
            console.log(g_ret);
        }

        onSuccess(g_ret);
    });

}


uploadImagesFbCounter(
    whateverAnzIs, 
    function(g_ret) {
        console.info(g_ret);
    }
);

respondido 10 mar '12, 23:03

You also need your $.ajax success callbacks to call onSuccess. I don't know jQuery, so you'll have to figure that out. - JoJo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.