1

As you may know it always have been a struggle to send custom HTTP requests from Flash apps. For instance if you have a Basic Auth protected webservice you won't be able to request it from Flash with GET request because only POST request can have custom headers. This is really troublesome.

I was wondering does anyone have tried using ExternalInterface to send HTTP request using Javascript? The idea would be to ask a JSmethod to send the request and pass the data back to the Flash.

Any thoughts?

htulipe
  • 1,575
  • 1
  • 10
  • 22

1 Answers1

3

Yes it is possible, I have done it a few times to use the Facebook JS sdk and pass back data to my swf.

Usually you call a JS function from your SWF with :

ExternalInterface.call("getFriends",params);

On the JS side :

function getFriends(params)
{
    FB.api('me/friends',function(response){
         mySwf.onGetFriendsComplete(response);
    });
}

On the SWF side to receive the call from JS :

ExternalInterface.addCallback("onGetFriendsComplete",onGetFriendsComplete);

private function onGetFriendsComplete(data)
{
   trace(data);
}

If you are running your SWF locally you might want to put checks before the ExternalInterface calls to make sure it is available :

if(ExternalInterface.available)
    ExternalInterface.call(...);
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • There is a security issue here though. When calling mySwf.onGetFriendsComplete(response); my Chrome sends me "Uncaught Error: SECURITY_ERR: DOM Exception 18" and "Uncaught Error: Error calling method on NPObject." – htulipe Feb 27 '13 at 11:07
  • http://stackoverflow.com/questions/1418528/javascript-flash-throwing-error-calling-method-on-npobject You might want to install the debug version of the flash player and test in firefox too to see whats going on. mySwf should be the object returned by the embed. – Barış Uşaklı Feb 27 '13 at 15:38