14
Apr
08

Calling a simple web service from Flex using the WebService class

The following example shows how you can call a ColdFusion Web Service from Flex using the <mx:WebService /> tag.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/14/calling-a-simple-web-service-from-flex-using-the-webservice-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.utils.ObjectUtil;

            private var startTime:int;
            private var endTime:int;

            private function button_click():void {
                webService.getMonths.send();
                startTime = getTimer();
                lbl.text = "";
            }

            private function getMonths_result(evt:ResultEvent):void {
                textArea.text = ObjectUtil.toString(evt.result);
                calcTime();
            }

            private function getMonths_fault(evt:FaultEvent):void {
                Alert.show(evt.type);
                calcTime();
            }

            private function calcTime():void {
                endTime = getTimer();
                lbl.text = "total time: " + (endTime - startTime) + "ms";
            }
        ]]>
    </mx:Script>

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

    <mx:ApplicationControlBar dock="true">
        <mx:Button id="button"
                label="get months from web service"
                click="button_click();" />
        <mx:Spacer width="100%" />
        <mx:Label id="lbl" />
    </mx:ApplicationControlBar>

    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="100%" />

</mx:Application>

View source is enabled in the following example.

If you’re using ColdFusion, the months web service is as follows:

months.cfc

<cfcomponent output="false">

    <cffunction name="getMonths" access="remote" returntype="array" output="false">
        <cfset var monthNames = ListToArray("January,February,March,April,May,June,July,August,September,October,November,December") />
        <cfreturn monthNames />
    </cffunction>

</cfcomponent>

8 Responses to “Calling a simple web service from Flex using the WebService class”


  1. 1 Apr 15th, 2008 at 2:47 am

  2. 2 Apr 15th, 2008 at 2:47 am

  3. 3 RBKB Apr 23rd, 2008 at 3:27 am

    I am calling a webservice using the same code as yours but it gives me following error:

    RPC Fault faultString= “Unrecognized binding style ‘null’. Only
    ‘document’ and ‘rpc’ styles are supported.” faultCode=”Encoding Error”
    faultDetail= “null”

    I am not able to find out What is the reason for this error? Can you please help?

    Could this be because the WSDL file I am using does not have style=”document” in . But is in the tag?

    Any options to this or any other thing that I can do ?

    Thanks in advance

  4. 4 RBKB Apr 23rd, 2008 at 3:29 am

    In the above comment i tried to say.

    The WSDL file I am using does not have style=”document” in soap:binding element. But is in operation tag. Could this be the reason why I am getting the above mentioned error?.

  5. 5 insane May 4th, 2008 at 11:08 pm

    question :

    flex dont let me define a operation name with a dot inside, what i should i do ?
    help.
    thnks

  6. 6 Shameer Jun 27th, 2008 at 12:08 am

    Thanks for the code.

    I tried to create a flex application, which is calling a webservice. It runs fine locally. When put on the server, I am getting an error.

    The fault string looks like

    message faultCode:Channel.Security.Error
    faultString:’Security error accessing url’
    faultDetail:’Unable to load WSDL. If currently online, please verify the URI and/or format of the WSDL (http://myshec103077d:8080/Check/addint?wsdl)’

    I tried putting crossdomain.xml in the root of the application server and I am able to access the cross domain file by typing
    http://myshec103077d:8080/crossdomain.xml

    The cross domain file which I had put is of the form

    Still I am getting the same error, when I run the flex app on the server.

    Can you please help me on this

    Thanks,
    Shameer.

  7. 7 PT Jul 21st, 2008 at 4:06 am

    Hello,

    Im using flex and calling a .net webservice to fill a data grid clickin on a button but i get an error, hope you can help me! Here is my flex code, webservice is working in asp normally and returns an array of objects!

    [RPC Fault faultString=”Unrecognized binding style ‘null’. Only ‘document’ and ‘rpc’ styles are supported.” faultCode=”EncodingError” faultDetail=”null”]
    at mx.rpc.soap::Operation/http://www.adobe.com/2006/flex/mx/internal::invokePendingCall()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\Operation.as:774]
    at mx.rpc.soap::Operation/send()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\Operation.as:688]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.rpc.soap.mxml::Operation/send()

  8. 8 PT Jul 21st, 2008 at 6:56 am

    Hi again,

    I forgot to say that error happens with WCF webservice. I tested with ASMX using the internet samples and all ran well!
    As i cant find WCF samples for Flex could you tell me if Flex doesnt support WCF or something like that? Thanks!

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".