Flex Examples
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.
<?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>
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
38 Responses to Calling a simple web service from Flex using the WebService class
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


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
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?.
question :
flex dont let me define a operation name with a dot inside, what i should i do ?
help.
thnks
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.
Sameer, I guess you are working on a system in Infosys Mysore :D
myshec :) I still remember the system IDs
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!
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!
Hi,
I am getting a query returned by the webservice. I am able to bind the returned query to gridview control. But how can I access the records returned one by one.
Eg. the query returned is name, age, salary. I want to get name in one variable, age in another and salary in third variable.
Akhilesh Srivastava,
I believe you’d have to loop over the result set and create the variables manually if you wanted them broken down by column (name/age/salary) instead of by rows.
Peter
Peter,
Thanks for your comment. Can you give me the code for this.
Akhil
Need to proxy webservice ate proxy-config.xml
Nice example
Hi,
Is there any way I could know all avilable operations at runtime?
(means not specifiying “hardcoded” the operation name by myself)
Thanks!
Hi,
The example doesn’t work in flash player 10.
When I click on the button in the example above, I get an alert with “Fault” back from the webservice…
Any idea?
Laurens
Hi,
I have the same problem as Laurens.
Can you help me?
WIllen
Hi There,
is there any way to load the wsdl address from an external text or xml file?
thansk
Ahmed
Ahmed,
Sure, I just copied my WSDL URL into an external text file, wsdlURL.txt, and then changed the <mx:WebService /> tag to the following code:
Peter
Thanks Peter for your help :-)
much appreciated
That was a valuable script thanks a lot man.
@PT – I encountered the same error. I found that changing the soap encoding on the WSDL to version 1 seemed to fix the issue.
It would appear that when the soap encoding on the WSDL is 1.2, things go wrong.
Hi Peter,
I wonder if it is possible to invoke a web servic call from a TitleWindow component which I am displaying as a popup window?
Thanks.
@Angelo,
I imagine that would work with no problems, but give it a try and let us know what you find out.
Peter
Hi Peter,
I tried to do a little example but the web service doesn’t seem to get called from a popup window.
The web service is successfully called from the main application but from the popup titlewindow, it does not work.
I placed some Alert message boxes before and after the web service is called as well as in the ResultHandler event. The alert message shows prior to calling the web service but those in the ResultHandler does not show, which leads me to conclude that the web service method is not being invoked.
I also placed some logging mechanism in the back end web service method and the web service call is logged when I invoke it from my main app, but from the popup, it does not seem to get invoked.
I wonder if this is a flex limitation. If not, I would love to hear from you on this.
Am using the SDK 3.4 by the way.
Thanks.
If interested in other simple example go to
http://programmaremobile.blogspot.com/2009/10/flex-as3-call-java-webservice.html
When you make a simple webservice, the result is simple to parse and bind to a String or Number object for example.
When you make a more complex webservice that transfer or receive by parameter a complex custom object, you need to bind the data to the object stub in your client.
By using Flex Builder you can do the binding work very easy.
click on the url in the signature in order to see the simple steps for do it.
@ Shameer
Even i have the same problem as yours. Is your isssue solved? If so,please help me out.
in flex 3, the menu sequence: Data – Import Web Serverice (WSDL) does a great job of importing the code necessary to turn web service calls into rather straightforward subroutine calls (almost)
And it works great in the development environment.
My web services are being hosted by IIS7 on Windows Server 2008. The SWF file is also being loaded from same.
The problem is that when transferred to a target machine, not the development machine, the code generated by the above “automated” process explicitly specifies the development machine, not the target machine.
There must be a simple and proper way to tell the generated code where the web services are ( hint: same machine that loaded the swf file )
Any suggestions?
/Boyd
Boyd,
I’m struggling with the same issue! Did you make any headway on it ?
I do not know the wsdl address so I use the following wsdl=”http://{getPageDomain}//webservice.asmx?wsdl”. But nothing is displayed. I am getting the page domain from url and splitting it. Please help.
When I try to run the application from this webpage, it just displays an alert window with “fault” . What might be the issue? Has anyone faced a similar problem?
@vik,
The problem is that one of my other sites, http://www.flash-mx.com, is down, and I haven’t bothered to look into it yet. So yes, the SWF example on this page is less than useful. Although the code itself should still be relevant to connecting to a different web service.
Peter
@Peter – Thanks a lot for your response. Yes the code is very useful and thanks again for it. I am trying to connect to a different webservice but I am getting the following error: “HTTP Request Error” although I am using a crossdomain.xml. Since its a HTTP req error, I am suspecting something to do from my side for making HTTP calls… network related issue. Any thoughts on this?
@vik,
I usually start debugging by creating a ColdFusion or PHP script which connects to the web service so I can test to see if the web service is potentially failing or if it is an ActionScript/Flex issue.
Peter
Thanks Pd, Will try that way.
hi,
I am calling a web service (web service is deployed in the different domain) from flex using flash player 9 (crossdomain.xml is placed in the application root directory)and it is working fine. But when we ran it on flash player 10 we got the error:
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
Please help me in solving this
Hi Peter,
Thanks for the stuff.
I just need a small piece of info from you.
I have a Action Script Value Object class, I’ve successfully passed and received in the Server side (Java). But from the Client side I’m not able to receive the Java Value object from Server Side. After surfing some sites, I’ve got the solution.
But the current problem is, I’m not able to read or receive the Array type values inside Java VO in the Client Side.
Please suggest some solutions.
Thanks
Nathan5.x
Hi,
I am using wsdl to get data but do not know how to send manipulated data back through WSDL. I am looking for your immense help
hi everyone, i am to flex and wcf.i am using wcf webservice, can anyone how to use wcf webservice in flex.. but i created the wcf webservice.. i dont know how to connect the wcf webservice. please can anyone help me its very urgent..pls.