Search code examples
javascriptajaxcoldfusion-9cfc

variable not being passed when making a call through cfajaxproxy to a cffunction


I found this question asked previously but it contained bad examples and no true answers so I am here to ask it again. First the code:

The HTML:

<td><input type="button" name="clear_#get_images.prdt_img_rel_ID#_cache" value="Clear Cache" onClick="clearCache('#get_images.images_name#');"/></td>

The Proxy:

<cfajaxproxy cfc="/cfc/cloudfiles" jsclassname="proxy">

The Javascript:

<script type="text/javascript">
    var proxy = new proxy();
    function clearCache( objectName ) {
        proxy.setCallbackHandler( purgeResultsHandler );
        proxy.setErrorHandler( myErrorHandler );
        alert(objectName);
        proxy.purgeItemDirectly( objectName );
    }

    var purgeResultsHandler = function ( res ) {
        alert( res );
    }

    var myErrorHandler = function(statusCode, statusMsg) {
        alert('Status: ' + statusCode + ', ' + statusMsg);
    }
    </script>

The CFC:

<cffunction name="purgeItemDirectly" access="remote" returntype="string" output="false">
    <cfargument name="container" type="string" required="false" default="content" />
    <cfargument name="objectName" required="true">
    <cfset var res = '' />

    <cfhttp method="DELETE" charset="utf-8" url="#variables.cdn_url#/#_encodeContainerName(arguments.container)#/#_encodeObjectName(arguments.objectName)#">
        <cfhttpparam type="header" name="X-Auth-Token" value="#variables.auth_token#" />
        <cfhttpparam type="header" name="X-Purge-Email" value="#Application.debuggingEmail#" />
    </cfhttp>

    <cfswitch expression="#ListFirst(cfhttp.statusCode, " ")#">
        ... code to iterate through responses ...           
    </cfswitch>
    <cfreturn res >
</cffunction>

As you can see, I have an alert to check the objectName in the JS. At this point the objectName does exist. However, it does not exist as soon as I get into the cffunction in the cfc. I have inserted text values to return into the cfc so I know it is getting called correctly, I simply cannot pass a value into it (either from a variable or just a plain string).

Thanks in advance for your help.


Solution

  • In your javascript, you're calling the function with a single argument (proxy.purgeItemDirectly( objectName )). Your CFC defines two arguments: container and objectName. So your javascript is passing your objectname argument into the container argument of the CFC. You need to make sure that you pass in both arguments, or you change the order of the arguments in your CFC to match what you expect from your JavaScript calls.