ActionScript ExternalInterface, jQuery, Web Service

I'm having some problems with ExternalInterface.call calling a javascript function which calls a web service.

I am trying to get a return value from a javascript function, but its not working out.

I suspect the problem is that, when I call the js function it calls the webservice and there is a delay. So the actionscript carrys on without waiting for the result.

So when I do: StateTextBox.text = String(retData); - it appears as undefined. The web service carrys out its functionality just fine and the database is updated as it should be.

Is there a way I can make the flash (ExternalInterface.call) wait for the result ? All help would be greatly appreciated - thanks.


The code:
----------------------------------------------

ActionScript2.0

CompletedButton.onRelease = function(){ UpdateGame(3); }

function UpdateGame(gameState) {

 var DBSaveSuccess:Boolean;
 if (ExternalInterface.available) {

   var retData:Object = retData = ExternalInterface.call("GameUpdate", "param1", "param1", gameState);

   StateTextBox.text = String(retData);

   if(Boolean(retData)) {

      ResultTextBox.text = "saved successfully";
   }
   else {
      ResultTextBox.text = "error saving, please contact ccteam@hideacase.com";
   }
  }
}

----------------------------------------------------------------

javascript

function GameUpdate(svt, gg, st) {
    
    var webMethod = 'http://someaddress.com/MyWebMethod';
    var parameters = "{'svt':'" + svt + "','gg':'" + gg + "','st':'" + st + "'}"

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {

            return msg.d;

        },
        error: function(e) {
            return false;
        }
    });

}

Open in new window

LVL 1
bLUEAsked:
Who is Participating?
 
moagriusCommented:
no.  you can use "synchronous" ajax instead, and not use success listeners.  otherwise, you'll need to send the data from javascript back to flash in your success/error handlers, and move the reaction to the return to another function that's called from there (with an addCallback invocation).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.