CF 8 Ajax with CFSELECT BIND

ColdFusion Add comments

I love the new AJAX functionality in ColdFusion 8, but I am running into problems using it.

As mentioned in Ben Forta's blog you can use the BIND= attribute on a CFSELECT tag to populate the data from a CFC using AJAX. 

The problem I had was that even using Ben's samples I could not get the data to populate.  After alot of tinkering I finally got it to work by remove the onRequest() method from Application.cfc.  Why this affects this functionality is a mystery to me.  Really for the application I'm working on it's not a big problem but on other projects it could be.

My second part of the problem is that I have a cfc directory that is OUTSIDE of the application's root directory.  For example:

/website_root

/components

/images

Then I map /cacfc to /components and reference my cfc's by prepending cacfc to the component IE. cacfc.database.data.   This works great as I can share components among applications easily enough.  But when I call the cfc with the bind attribute of the CFSELECT, ColdFusion looks for the cfc in the root directory.   Here's an example:

<cfform name="schooldata">

<table>
<tr>
<td>Select State:</td>
<td><cfselect id="state_code" enabled="true" name="state_code" bind="cfc:cacfc.database.school.getStates()" bindonload="true" value="state_code" display="state_name"></cfselect></td>
</tr>
<tr>
<td>Select County:</td>
<td><cfselect name="county" bind="cfc:school.getCountiesInState({state_code})" value="county_name" display="county_name" /></td>
</tr>
</table>

</cfform>

The AJAX debugger looks for the cfc /school.cfc which obviously doesn't exist.   If I move the cfc to a directory under the web root (ie /cfc) it works.

Either I am doing something terribly wrong or these are 2 bugs in CF8. 

5 responses to “CF 8 Ajax with CFSELECT BIND”

  1. Sid Maestre Says:
    Thanks for the post on binding to a cfselect. Have you found out why the OnRequest function in the application.cfc file interferes with cfselect data binding? I was having the same problem and your solution worked for me.

    I found a work around using bind="url:ajax.cfm?action=getStuff()" as a substitute for bind="cfc:cfc.foo.getStuff()"

    I prefer calling the cfc directly for security reasons.
  2. Matt McDonald Says:
    Sid,

    I agree with you on calling the CFC directly. The URL method seems more like a hack to me, but ....

    On the onRequest(), Ben Forta's reply was:

    "The onRequest() issues sounds sounds like the same limitation with Web Services and more, and is documented as a restriction."

    Make sense but a bit limiting.
  3. John C. Bland II Says:
    Matt, as you pointed to Ben's quote, the onRequest method is problematic for web services, Flash Remoting, etc. The problem is you are trying to call "whatever.cfc" and onRequest is attempting to serve the file to the browser vs allow a direct call to a method int he cfc.

    To get around it use onRequestStart or do a check to see if the requested filename is your remote cfc.
  4. Chris Says:
    You can deal with this dynamically at runtime, and still keep onRequest for cfm pages

    <cffunction name="onRequestStart">
       <cfargument name="targetpage">
       <!--- Remove the onRequest method for remote cfc calls, otherwise we'll get a blank response --->
       <cfif listlast(arguments.targetpage, ".") is "cfc">
          <cfset StructDelete(this, "onRequest") />
          <cfset StructDelete(variables,"onRequest")/>
       </cfif>   
    </cffunction>
  5. gowri Says:
    i want to select one select option then it efect will show on to the another select option box

Leave a Reply



Powered by Mango Blog. Design and Icons by N.Design Studio