4 Ways to Invoke a WebService in Flex with MXML and ActionScript

| 2 Comments

I had a recent need to call a webservice to receive some display data. I thought the task was quite easy and in reality it is but trying to Google webservice and Flex brings you back more information than you want. I've attached an example of 4 ways to call a webservice in Flex.

Here is the file: TestWebService.mxml.

Solution #1) Webservice via MXML

My first solution was really easy; I found out how to call a webservice using MXML and I had a test web service running in no time.

<mx:WebService id="wsMXML" wsdl="http://www.flash-mx.com/ws/months.cfc?wsdl">
     <mx:operation name="getMonths" resultFormat="object" result="result1(event);" fault="fault(event);" />
</mx:WebService>

Then call "wsMXML.getMonths.send();"

And your result handler is:

var myObj:ArrayCollection = evt.result as ArrayCollection
textArea1.text = myObj[4].toString();

Problem was I found out I needed to do it in ActionScript. Hence solution #2.

Solution #2) Webservice via ActionScript

My next solution took me a little longer to figure out. I tried importing the WSDL via the Import Web Service feature in Flex Builder. Here's how:

- Click on: "Data | Import Web Service"
- Select the folder where the flex source code resides.
- Type in the name of the URL of the WSDL
- Click "Finish" or configure the call a little more on the next screen and then select "Finish".

This creates proxy classes for your webservice, so now you can call them just like your ActionScript classes. I used this WSDL http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?wsdl and then had to use this code:

var myWS:EmailVerNoTestEmail = new EmailVerNoTestEmail();
myWS.addverifyEmailEventListener(result3);
myWS.verifyEmail("sjobs@apple.com", null);

With this result handler:

var myObj:Object = evt.result;
textArea3.text = myObj.GoodEmail.toString();

Pretty neat... but then I found out that my WSDL was behind a firewall that was not accessible from any external systems. I had to come up with a new solution to load the webservice at runtime. Hence solution #3.

Solution #3) Webservice via ActionScript with a parameter

Same as one above but it sends a parameter.

Solution #4) Webservice at Runtime via ActionScript

My last solution worked (assuming you set up the cross-domain.xml correctly) the best for my situation.

var loginWS:WebService = new WebService();
loginWS.useProxy = false;
loginWS.VerifyEmail.addEventListener("result", result4);
loginWS.LoginOperation.resultFormat = 'e4x';
loginWS.addEventListener("fault", fault);
loginWS.loadWSDL('http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?wsdl'); loginWS.VerifyEmail("sjobs@apple.com", "?");

With this result handler:

var myObj:Object = evt.result as Object;
textArea4.text = myObj.ResponseText.toString();

 

2 Comments

Hi, thanks so much for your sharing.. it's very helpful!

I got one question: how to setup the cross-domain.xml correctly?

Setting up a cross-domain policy "correctly" is a bigger conversation and probably a blog in itself. What I believe to be true is that you must set up a cross-domain policy at the root of your web application server and then you can set up another cross-domain policy within each web application. If you're just doing development then you could just set up a cross-domain.xml file in your web root folder and allow all.

For web services, you would




For simple HTTP requests/responses, it would look like this…



Leave a comment

Categories