Search code examples
javascriptactionscript-3flashexternalinterface

Javascript not returning data to AS3 flash


I am hoping someone can help me. I have the following function in FlashBuilder 4 using AS3:

protected function doIt():void
    {
        if (ExternalInterface.available) {
            var retData:String;
            retData = ExternalInterface.call("returndata");
            if(retData != null) {
                eidLbl.text = retData.toString();
            } else {
                eidLbl.text = "Returned Null";
            }
        } else {
            eidLbl.text = "External Interface not available";
        }                
    }

and the following javascript function it calls:

var fql_query = "some query format";
function returndata(){
    return fql_query;
}

When I run these from my site everything works fine! The flash file calls the javascript function and returns "some query format" into the proper text field.

However if I change my flash function to be as follows:

protected function doIt():void
{
    if (ExternalInterface.available) {
        var retData:String;
        retData = ExternalInterface.call("runFQL",uidLbl.text);
        if(retData != null) {
            eidLbl.text = retData.toString();
        } else {
            eidLbl.text = "Returned Null";
        }
    } else {
        eidLbl.text = "External Interface not available";
    }                
}

and the following javascript function:

function runFQL(id){
    var user_id = id;
    var page_id = "**********"; // the page id that is actually there is valid
    var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;

    FB.api({
        method: 'fql.query',
        query: fql_query
    },
    function(response){
        if (response[0]) {
                var uid = response[0].uid;
                window.alert(uid); //100001632043058
                return uid;
        } else {
                window.alert("return 1000");
        }
    });    
};

My problem is that when I run this one, it passes the id in, runs the query, and accurately pulls back the uid and displays it in the alert BUT (AND HERE IS MY PROBLEM) it won't pass it back to flash in the return. It comes back as null or undefined each time.

Can anyone tell me what I am missing. I have been scouring google for hours.


Solution

  • function(response){
        if (response[0]) {
            var uid = response[0].uid;
            window.alert(uid); //100001632043058
            return uid;
        } else {
            window.alert("return 1000");
        }
    });
    

    Is called when the FB.api function completes not when you call runFQL. So runFQL runs through and returns undefined everytime.

    What you can do is once the FB.api call completes call a function inside the flash app and pass the return value there. So basically do a ExternalInterface.addCallback in flash and call that function from javascript.

    function runFQL(id){
        var user_id = id;
        var page_id = "**********"; // the page id that is actually there is valid
        var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;
    
        FB.api({
            method: 'fql.query',
            query: fql_query
        },
        function(response){
            if (response[0]) {
                    var uid = response[0].uid;
                    window.alert(uid); //100001632043058
                    // needs ExternalInterface.addCallback("sendDataBack",handleData); in your 
                    // as3 code
                    yourSwf.sendDataBack(uid); 
            } else {
                    window.alert("return 1000");
            }
        });    
    };