Using HTTPService tag to send/receive variables to a server-side script
The following example demonstrates a very simple usage of sending parameters from Flex to a server-side script (written in ColdFusion) and then displaying the server response in our Flex application. The server-side script is a simple echo/”Hello world” type script, but it accepts URL or FORM variables, so you can send using the GET or POST HTTP-method.
It’s a pretty basic/crude example, but maybe it helps somebody out there.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/16/using-httpservice-tag-to-sendreceive-variables-to-a-server-side-script/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="httpService.send(myObj);">
<!-- Parameters to send to remote script. -->
<mx:Object id="myObj" name="peterd" />
<mx:HTTPService id="httpService"
url="http://www.flash-mx.com/mm/greeting.cfm"
method="POST"
resultFormat="flashvars" />
<mx:Label text="{httpService.lastResult.welcomeMessage}" />
</mx:Application>
View source is enabled in the following example.
By popular request, here is the ColdFusion source code as well.
http://www.flash-mx.com/mm/greeting.cfm
<cfsilent>
<cfsetting enablecfoutputonly="Yes">
<cfif IsDefined("URL.name")><cfset Form.Name = URL.name /></cfif>
<cfif NOT IsDefined("Form.name") OR Len(Trim(Form.Name)) EQ 0>
<cfset Form.Name = "[Mysterious Stranger]" />
</cfif>
</cfsilent><cfcontent reset="true" /><cfoutput>welcomeMessage=#UrlEncodedFormat("Welcome, "&Form.name)#</cfoutput>
<cfsetting enablecfoutputonly="No">
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.
0 Responses to Using HTTPService tag to send/receive variables to a server-side script
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


hi! nice to see this app
How do I have to different buttons, instead of putting this code (ie., click=”xmlRPC.send();” ). I want to call 2 different functions. One will pass hidden textinput field value as “1″ or “2″. This will tell CFMX to one of the 2 given CFC. httpservice calls a cfm page which cfinvokes one of the 2 cfc’s. httpservice use “POST” method.
Hi! Do you have the .cfm file? Is it possible if i take a look? Because i’m having problem how to write it.
Mas,
I updated the entry above, hope that helps.
Peter
Hello Peter,
Do you feel this would be a good way to send variables triggered by Cue Points in a flash video. I want to set a coupel cue points in a flash video and when they are triggered send a message that eventually chages out content in a HTML document.
IE Content specific information…similiar to what you expect from CC but rich media.
So a flash or Flex player is playing laong, hits a Cue Point which triggers my object and passes it a URL string. As above it sends it via MyObj to a MySQL database in my case. Then a JS object is listening for the object and populates a given content area with a SWF, JPG, Text or whatever that is in the data row…
Am I on the right track? I have been trying to run this down for a week…spent all last night reading up on External Class…
This has been a trying experience to fully understand how HTTPService and the supporting RPC (remote procedure call) classes operate. For those of you still having trouble, here is a solution I got working in Flex 3 returning simple XML after sending a login form using the POST method. It’s server side language independent.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; private function loginResult(event:ResultEvent):void{ // HTTP Service Succeeded trace("Success " + event.type); } private function loginFault(event:FaultEvent):void{ // HTTP Service Failed trace(event.type); trace(event.fault); } ]]> </mx:Script> <!-- resultFormat="e4x" was a huge step forward for me In this example my server receives the POST form variables email and password and validates the user to return the user_id and email_address . The output from the server page that was POSTed to returns the following exactly as it is here: <results> <user_id><![CDATA[ 1 ]]></user_id> <email_address><![CDATA[ dylan.valade@pinelakedesign.com ]]></email_address> </results> --> <mx:HTTPService resultFormat="e4x" method="POST" fault="loginFault(event)" result="loginResult(event)" url="https://www.yourdomain.com/login/" id="loginService" > <mx:request> <email>{email.text}</email> <password>{password.text}</password> </mx:request> </mx:HTTPService> <mx:VBox> <mx:TextInput id="email" text="dylan.valade@pinelakedesign.com" /> <mx:TextInput id="password" text="secret" /> <mx:Button click="loginService.send();" label="Login" /> <mx:Text id="resultText" text="Result From Server: {loginService.lastResult.email_address}" > </mx:Text> </mx:VBox> </mx:Application>Dylan-
Thanks for example.
Few ways in which variable can be passed to flex –mainly foccused for http request variables
http://blog.flexexamples.com/2007/08/16/using-httpservice-tag-to-sendreceive-variables-to-a-server-side-script/
Peter/Dylan-
Thanks for example.
Few ways in which variable can be passed to flex –mainly foccused for http request variables
http://yasob.blogspot.com/2009/05/accessing-http-request-parameter-in.html
I need to say this: THANKS!!!… I’ve been trying so hard to find out how to get a single result from an xml text recieved by a servlet, on flex, and I finally found your example and IT WORKS!!! I’m so excited that I really want to thank you… BIG help!!