The title kind of says it all. Most people have heard, or unfortunately ran into, the issue with Application.cfc and onRequest when used for Ajax or Flash Remoting calls. It just plain doesn't work. You don't get a nice error either and if you haven't heard of this bug, your going to be quite confused.
The good news is that ColdFusion 9 completely fixes this. Want to use onRequest and still work with CFCs? No problem. An Ajax/Flash Remoting/Web Service call to a CFC within a directory using onRequest will simply ignore the method! Consider this simple example using a new script based Application.cfc:
2
3 this.name="FixToOnRequest";
4
5 public boolean function onRequestStart(string target) {
6 writeLog(file="cfcfix",text="Running onRequestStart for #arguments.target#");
7 return true;
8 }
9
10 public boolean function onRequest(string target) {
11 writeLog(file="cfcfix",text="Running onRequest for #arguments.target#");
12 include arguments.target;
13 return true;
14 }
15
16 }
2
3 remote string function sayHello() { return "Hello World"; }
4
5 }
For my CFC, I got:"Information","web-15","07/13/09","06:46:21","FIXTOONREQUEST","Running onRequestStart for /test.cfm"
"Information","web-15","07/13/09","06:46:21","FIXTOONREQUEST","Running onRequest for /test.cfm"
Notice that onRequest didn't run at all. ColdFusion 9 also adds onCFCRequest. This works very much the same way as onRequest. It gives you complete control over the CFC request. If you don't actually run the method yourself, then, well, it isn't run. Much like how onRequest forces you to really include the file. onCFCRequest is sent three arguments: CFC Name, CFC Method, and a structure of arguments. Invoking a dynamic CFC and method is a bit tricky in an all script CFC. This is how I did it, but I'm willing to bet there is a simpler way:"Running onRequestStart for /test.cfc"
2 writeLog(file="cfcfix",text="Running onCFCRequest for #arguments.cfc#, method #arguments.method#, args: #structKeyList(arguments.args)#");
3 var comp = createObject("component", arguments.cfc);
4 var res = evaluate("comp.#arguments.method#(argumentCollection=arguments.args)");
5 if(isDefined("res")) writeOutput(res);
6 return true;
7 }
"Running onCFCRequest for test, method sayhello, args: zoo,foo"
I'll be honest - even without the onRequest bug, I didn't use it very often. Now that it's fixed though I won't feel so inclined to avoid it. onCFCRequest could be very useful for logging requests to an API and seeing which methods get used more often.
Comment 1 written by Sean Coyne on 13 July 2009, at 6:59 AM
You mean onRequest didn't run at all.
Comment 2 written by Raymond Camden on 13 July 2009, at 8:12 AM
Comment 3 written by Andy Sandefer on 13 July 2009, at 1:28 PM
Where you have 009 stored in the db but the grid displays 9?
@Ray - I know that you've blogged on this in CF8 but I'm just curious as to whether or not this is going to be a non-issue going forward.
Comment 4 written by Raymond Camden on 13 July 2009, at 1:30 PM
<cfset s = "009">
<cfoutput>#serializeJSON(s)#</cfoutput>
Still returns 9.0.
Comment 5 written by Aaron on 14 July 2009, at 11:12 PM
Comment 6 written by Raymond Camden on 14 July 2009, at 11:16 PM
[Add Comment] [Subscribe to Comments]