4 Replies Latest reply on Aug 10, 2011 1:39 PM by DFix1

    ExternalInterface.addCallback doesn't seem to work.

    DFix1

      Continuing from this forum post here:
      http://forums.adobe.com/thread/885200?tstart=0

      I am having trouble getting the ajax result back into flex... as an event so i don't need to keep checking in flex. from what i understand a callback is needed so I have set up a callback with the relevant flex portions here:

       

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     xmlns:bridge="bridge.*"
                     minWidth="955" minHeight="600" creationComplete="application1_initializeHandler(event)">

       

          protected function application1_initializeHandler(event:FlexEvent):void
                  {
                      ExternalInterface.addCallback("EICallBack", EICallBack);

      }
      protected function EICallBack(Result:String)
                  {
                      output = Result;
                  }

      protected function button1_clickHandler(eventLMouseEvent):void)

      {

      ExternalInterface.Call("ExecuteFunction", Value);

      }

      On the javascript side
      I created a delagate function
      function ExecuteFunction(arr)

      {

      testFunction(arr).EICallBack(output) //where output is set output = xmlHttp.responseText instead of the alert with a subsequent return of output

      }

       

      but the EICallBack is never executed.
      I have looked at other examples and they look quite similar. I do not know what i am doing wrong.  Can anyone help?

        • 1. Re: ExternalInterface.addCallback doesn't seem to work.
          Flex harUI Adobe Employee

          3 most common problems with addCallback:

           

          1) You aren't calling on the right object.  In your case, testFunction(arr)

          would need to return the Object tag that contains the Flash Player

          2) You are making the call before addCallback ran in the SWF

          3) You loaded the SWF from a different domain than the html page

          1 person found this helpful
          • 2. Re: ExternalInterface.addCallback doesn't seem to work.
            DFix1 Level 1

            After looking at your suggestions I made some changes to HelloFunction but it still fails to execute the callback.

            <script>

            output = "notset"

            function ExecuteFunction(arr)

             

            {

             

                var xmlHttp = new XMLHttpRequest();

            var url;

             

               url = "Location/to/server";

             

               var body = '{"input":arr}'; xmlHttp.open("POST", url, true);

             

                xmlHttp.setRequestHeader("Content-Type", "application/json");

             

                xmlHttp.onreadystatechange = function () {

             

                    if (xmlHttp.readyState == 4) {

             

                        output = xmlHttp.responseText;

            if(navigator.appName.indexOf("Microsoft") != -1)
                     {
                        return window["testFunctions"];  //testFunctions is the name set in the attributes.id field when defining the flash element so i took a guess
                      }
                       else{
                        return document["testFunctions"];
                       }

             

             

                    }

             

                };

             

                xmlHttp.send(body);

             

            }

            function helloFunction(arr)

            {

            ExecuteFunction(arr).EICallBack(output)

            }

             

            the callback is still set like this:
            protected function application1_initializeHandler(event:FlexEvent):void
                        {
                            ExternalInterface.addCallback("EICallBack", EICallBack);
                           
                            trace("INIT");
                           
                        }
            and
            protected function EICallBack(Result:String)
                        {
                            output = Result;
                            Alert.show(output);
                        }
            the trace is executed before running helloFunction so i am fairly sure that addCallback is executed before helloFunction runs.  And the swf file  and html page are on the same domain.
            Since i am running this on my development machine, could this be blocked by flex/flash if it is all run locally?
            is the name that i chose for the document return statement correct?
            I should mention that the application does give me the result and is stored in output nicely.

            • 3. Re: ExternalInterface.addCallback doesn't seem to work.
              Flex harUI Adobe Employee

              What does ExecuteFunction(arr) do?  Does it return the object containing the

              player?

              • 4. Re: ExternalInterface.addCallback doesn't seem to work.
                DFix1 Level 1

                d'oh! i realised i put the return statement in the wrong place.  Sorry and Thanks.