Passing parameters to an HTTPService
The followig example shows how you can pass parameters to an HTTPService by passing an Object in the HTTPService’s send() method. The remote ColdFusion script is a simple “hello world” type script which accepts a single parameter, “name”, and returns a single parameter, “welcomeMessage”.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/29/passing-parameters-to-an-httpservice/ -->
<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.events.ValidationResultEvent;
import mx.utils.StringUtil;
private function submit_click(evt:MouseEvent):void {
var result:ValidationResultEvent = stringValidator.validate();
var params:Object = {};
lbl.text = "";
switch (result.type) {
case ValidationResultEvent.INVALID:
Alert.show(result.message, result.type);
break;
case ValidationResultEvent.VALID:
params["name"] = StringUtil.trim(firstName.text);
httpServ.send(params);
break;
}
}
]]>
</mx:Script>
<mx:StringValidator id="stringValidator"
source="{firstName}"
property="text"
minLength="2"
maxLength="{firstName.maxChars}" />
<mx:HTTPService id="httpServ">
<mx:resultFormat>flashvars</mx:resultFormat>
<mx:url>http://www.flash-mx.com/mm/greeting.cfm</mx:url>
<mx:result>lbl.text = httpServ.lastResult.welcomeMessage;</mx:result>
<mx:fault>Alert.show(event.toString(), event.type);</mx:fault>
</mx:HTTPService>
<mx:ApplicationControlBar dock="true">
<mx:Form>
<mx:FormItem label="Name:" required="true"
direction="horizontal">
<mx:TextInput id="firstName"
maxChars="20" />
<mx:Button label="Submit"
click="submit_click(event);" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<mx:Label id="lbl" fontSize="32" />
</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.
23 Responses to Passing parameters to an HTTPService
-
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 tutorial. Can you please the same exaple using java as remote script to handle the request. Also do upload the script code as well. This will give clear picture
Thank’s
Hi! Do you have the .cfm file? Is it possible if i take a look? Because i’m having problem how to write it.
Hi, I have a question about <mx:url>http://www.flash-mx.com/mm/greeting.cfm</mx:url>
I want to replace this URL with my local url like:http://localhost:8080/baidusongs/greeting.cfm
and the file content of greeting.cfm is:
welcomeMessage=Welcome%2C%20%5BMysterious%20Stranger%5D
but it doesn’t work; error message is as bellow:
[FaultEvent fault=[RPC Fault faultString="Security error accessing url" faultCode="Channel.Security.Error" faultDetail="Destination: DefaultHTTP"] messageId=”35B47C6E-A306-19F6-5CB9-3A6DB07123C8″ type=”fault” bubbles=false cancelable=true eventPhase=2]
thanks for your time!
yes, maybe i have the same problem with Mas.
Thanks…
Mas,
I updated the entry above, hope that helps.
Peter
Liceven,
You may be getting a security error if your SWF/HTML is using the file:/// protocol and your webservice/CFM is running on http://.
I’d start by copying the contents of the /bin-debug/ or /bin-release/ folders into the same directory as the .CFM file and view the sample over HTTP.
Peter
Hello,peterd,
I copy all the files in folder /bin-debug/ to the same directory as the .cfm. And view the swf over http. It don’t pop up the error dialog this time, but it doesn’t display welcome information either.
And i viewed the greetinng.cfm using IE browser. I found the return contents are different.
http://www.flash-mx.com/mm/greeting.cfm—>welcomeMessage=Welcome%2C%20%5BMysterious%20Stranger%5D
whiel http://localhost:8080/flexweb/greeting.cfm returns welcomeMessage=#UrlEncodedFormat(“Welcome, “&Form.name)#
why? i have little knowledge about coldfusion. so, thanks very much.
Liceven,
Looks like your ColdFusion server isn’t parsing the .CFM templates for some reason. Which version of ColdFusion did you install?
Peter
ColdFusion MX 7
Maybe there is something wrong with the configuration.
Thanks very much Peterd.
I am having problem using HTTPService, Can u please help me for the problem mentioned on the link below?
http://board.flashkit.com/board/showthread.php?t=772909
It’s broken, I get a fault “channel security error”.
Yeah, looks like http://www.flash-mx.com is down.
Hi ,
Can u specify an example how to write httpservice in actionscript
Hello, nice tutorial!
but why flashvars????
Tks!
Hi,
Your site has been really helpful to learn Flex, appreciate your time. I’m learning flex and I’m stuck here, not sure how to debug this issue. I do not get any error, but the DB is not getting inserted with the value. Any help on fixing the issue, will be great.
Below are the key codes:
submit button code:
addAnnouncement function code:
private function addAnnouncement( evt:Event) :void{
var selArr:Array = [];
var oRequest:Object = new Object();
var idx:int;
var len:int = userRequest. lastResult. partners. partner.length;
for (idx=0; idx
INSERT INTO tblAnnouncement
(strTitle, strContent, dtmOpenDate,
dtmCloseDate, dtmAssignmentDueDat e )
VALUES
(,
,
,
,
)
i’m stuck here..not sure, where to look for the bug..i do not see
any error..any help is really appreciated.
-NB
BTW, I had a problem in which I tried to pass an XML object as a parameter using Flex 3 and it wouldn’t appear at the server. What I did was to create another object with its only key-value pair containing the string representation of the XML object.
Thanks for the blog.
http://www.flash-mx.com/mm/greeting.cfm?name=testName1&name=testName2&name=testName2
how can i send request parameters with the same name?
jayanthi,
I’ve never tried that. It may depend on the server configuration. I imagine sending X number of parameters with the same name will either overwrite the previous parameter’s value, or else if you’re lucky the server may convert the duplicate parameters into an array/list (so the server may convert the name to “testName1,testName2,testName2″.
You’d have to experiment with your specific server and see what the behavior is.
Peter
it will overwrite. if you need the multiple name you need to serialize the value
I have a problem with the latin character, I can´t insert into data base mysql the values á,í,,,,,,,ñ,etc. Can you help me.
I typed “Ação”
The return was: AÃ…
How to troubleshoot problems with Latin characters?
thanks, your examples are great . has saved me lots of times and thought me lots of things . but if you add some comments to your codes that explains the code it would be lot more understandable .
thanks
Can u tell about the data base connectivity in Flex?