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"?>
<!-- -->
<mx:Application xmlns:mx=""

            import mx.controls.Alert;
            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:
              , result.type);
                    case ValidationResultEvent.VALID:
                        params["name"] = StringUtil.trim(firstName.text);

    <mx:StringValidator id="stringValidator"
            maxLength="{firstName.maxChars}" />

    <mx:HTTPService id="httpServ">
        <mx:result>lbl.text = httpServ.lastResult.welcomeMessage;</mx:result>
        <mx:fault>, event.type);</mx:fault>

    <mx:ApplicationControlBar dock="true">
            <mx:FormItem label="Name:" required="true"
                <mx:TextInput id="firstName"
                    maxChars="20" />
                <mx:Button label="Submit"
                        click="submit_click(event);" />

    <mx:Label id="lbl" fontSize="32" />


View source is enabled in the following example.

By popular request, here is the ColdFusion source code as well.

<cfsetting enablecfoutputonly="Yes">
<cfif IsDefined("")><cfset Form.Name = /></cfif>
<cfif NOT IsDefined("") OR Len(Trim(Form.Name)) EQ 0>
    <cfset Form.Name = "[Mysterious Stranger]" />
</cfsilent><cfcontent reset="true" /><cfoutput>welcomeMessage=#UrlEncodedFormat("Welcome, "&</cfoutput>
<cfsetting enablecfoutputonly="No">

23 thoughts on “Passing parameters to an HTTPService

  1. 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


  2. Hi! Do you have the .cfm file? Is it possible if i take a look? Because i’m having problem how to write it.

  3. Hi, I have a question about <mx:url></mx:url&gt;
    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:
    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!

  4. 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.


  5. 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.—>welcomeMessage=Welcome%2C%20%5BMysterious%20Stranger%5D
    whiel http://localhost:8080/flexweb/greeting.cfm returns welcomeMessage=#UrlEncodedFormat(“Welcome, “&
    why? i have little knowledge about coldfusion. so, thanks very much.

  6. 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 )


    i’m stuck here..not sure, where to look for the bug..i do not see
    any error..any help is really appreciated.


  7. 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.

  8. 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.


  9. I have a problem with the latin character, I can´t insert into data base mysql the values á,í,,,,,,,ñ,etc. Can you help me.

  10. 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 .

Comments are closed.