Calling a WebService Purely through AS

On the flex coders list, folks often ask if its possible to call a WebService purely with ActionScript.  Its been asked so often, there is even an entry in the FAQ about it.  According to the official word from Macromedia, there is no supported mean to create a WebService purely through ActionScript,  however, it is actually possible, just not supported.  My standard disclaimer (see TabNavigator article) about using unsupported code applies.

Actually, its not really that hard to do.  A simple class to instantiate a WebService looks like this:

class ASWebService extends mx.core.UIObject{
    private var remoteDataService:mx.servicetags.Service;

      private function init():Void {
          super.init();
          remoteDataService = new mx.servicetags.Service(this._url, "http://localhost:8500/mmcourses/faad2004/dbs/Bikeparts.cfc?wsdl", new mx.services.Log(1, "WebService"), null, this, null, null, null, null, false);
           remoteDataService.__name = "remoteDataService";
           remoteDataService.__faultHandler = function(event) {
               faultHandler(event)
           }
           remoteDataService.__resultHandler = function(event){
                resultHandler(event)
          }
          remoteDataService.__showBusyCursor = true;
      }
      // event handlers
      private function faultHandler(event){
          mx.controls.Alert.show(event.faultstring);
     }
     private function resultHandler(event){
           mx.controls.Alert.show(event.result.length + " results received");
      }
      // public API
     public function makeCall(method:String){
          remoteDataService[method]();
     }

}
This can be instantiated in MXML like this:

<mx:Application
 xmlns:mx="http://www.macromedia.com/2003/mxml"
 xmlns:c="*" creationComplete="dm.makeCall('getProdTypes')">
 <c:ASWebService id="dm"/>
 
</mx:Application>

Word on the street is there will likely be a supported way to do this in Flex 2.0, but like talk about any unreleased product, at this point it is no more than a rumor.

I’ve put together a supported way to do this in Flex 2.0, which you can read about in my AS3 DataManager post.

There are no comments.

Leave a Reply