Ask a Jedi: Ajax-bound requests and onRequest

This post is more than 2 years old.

So this is definitely something I've covered more than once, but as the questions keep coming in, I'm going to keep blogging it. My real hope is that Adobe listens in here as I think this is something that really needs changing in ColdFusion. Anyway, the question, from Samer:

I found a wired case, if you use onRequest function in Application.cfc and use cfgrid with bind attribute, it always return empty query to the grid "Response is Empty" !!

Can you help?

This is a fairly simple problem. onRequest, if it exists, breaks all flash remoting and CFC calls (remote calls I mean). Make sure you understand that. It doesn't matter what you do in onRequest - the mere existence of the method will call Flash Remoting/CFC (Remote) operations fail. Period.

As I said, this is something I truly wish Adobe would just fix as all it does is trip people up.

There is a way around it. Sean Corfield has a nice little work around that involves adding the following code to your onRequestStart (not onRequest, but onRequestStart):

<cfif listlast(arguments.thePage,".") is "cfc"> <cfset StructDelete(this, "onRequest") /> <cfset StructDelete(variables,"onRequest")/> </cfif>
Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Will Swain posted on 3/19/2008 at 8:13 PM

Thanks Ray, I missed that post of Sean's the first time round.

As someone who has run into this problem a few times, I have to say I consider this a bug. I wonder if Adobe do too.

Comment 2 by Sid Wing posted on 3/19/2008 at 9:26 PM

BIG BIG Bug! Adobe needs to break out the flyswatter for sure on that one.

Comment 3 by Michael White posted on 3/20/2008 at 9:11 PM

more than a bug, it's a scorpion

Comment 4 by Don Li posted on 3/22/2008 at 5:05 AM

I get the impresson that application.cfc and CFC components may not play together that well for cf8 (my copy is standard). Hence, I'm a bit relunctant to change the application.cfm to application.cfc for one app which uses a handful of CFCs. I understand I may lose some efficiency...

Is Adobe all consumed with Flex and Air now?

Thks.

Comment 5 by Raymond Camden posted on 3/22/2008 at 7:37 PM

Don, I'm not sure where you got this impression. There is absolutely nothing wrong with using CFCs and Application.cfc. You lose nothing moving from App.cfm - instead you gain new features.

Comment 6 by Don Li posted on 3/22/2008 at 10:57 PM

ok, Thank you, Ray. I'll remove my hesitation and move forward with application.cfc then.

Don

Comment 7 by Don Li posted on 4/2/2008 at 4:30 AM

But Ray, I have a question, how to translate the following
code,
<cfscript>
daysForData = 3;
</cfscript>
in the application.cfm framework
to its equivalent in the application.cfc framework?

Also, it would be referenced by many (15+) templates.

Many thanks.

Comment 8 by Raymond Camden posted on 4/2/2008 at 5:27 PM

That code in APp.cfm would be equivalent to the same code in onRequest.

Comment 9 by GM posted on 7/24/2008 at 10:46 PM

Hi Ray, I am trying to build an application and I am extending an Application.CFC my code uses onRequest in the extended Application.CFC and onRequest breaks my binding in cfselect? I tried your work around from Sean C. I am not clear on where arguments.thePage is referring to. Any help would be appreciated.

Comment 10 by Raymond Camden posted on 7/25/2008 at 5:25 AM

Well arguments.thePage is passed to onRequest automatically. It's the page being requested. So Sean's fix is to look at the value, and if it "something.cfc", to remove itself from the request.

Comment 11 by GM posted on 7/25/2008 at 1:31 PM

Thanks Ray, so you are saying that arguments.thePage gets passed automatically. So, my question then is the name of the variable always thePage? Or could it be something else? And if it is something else how where in the code is this determined?

Comment 12 by Raymond Camden posted on 7/29/2008 at 6:08 AM

The variable name is based on what you use in the cfargument tag. If you had cfargument name="myfreakingpage", it would be arguments.myfreakingpage.

Comment 13 by GM posted on 7/29/2008 at 4:07 PM

So, Ray sorry for being so dense on this. You mean the cfargument tag name attribute that you are passing to the onRquest method correct?

Comment 14 by Raymond Camden posted on 7/29/2008 at 4:11 PM

Right - the argument names are up to you.

Comment 15 by Sasa posted on 10/7/2008 at 2:45 PM

If You dont want to deal with argument name you can also use

<cfif listLast(CGI.CF_TEMPLATE_PATH, ".") is "cfc">

I am using this in my Application.cfc and its working. I use this becouse listlast(arguments.thePage,".") in onRequestStart returned error.

Comment 16 by Steve posted on 3/19/2009 at 2:57 AM

Ray, I am having a similar problem related to this topic. However, I'm not using the onRequest/onRequestStart and there is no application.cfc.

When I load my grid page, I get a popup with this error:

Error parsing JSON response: <wddxPacket version='1.0'><header/><data><struct><var name='TOTALROWCOUNT'><number>3.0</number></var><var name='QUERY'><recordset rowCount='1' fieldNames='ID,LOGINID,LOGINPW,LOGINNAME,LOGINLEVEL' type='coldfusion.sql.QueryTable'><field name='ID'><number>3.0</number></field><field name='LOGINID'><string>jaime</string></field><field name='LOGINPW'><string>xXxX</string></field><field name='LOGINNAME'><string>Jaime</string></field><field name='LOGINLEVEL'><string>Admin</string></field></recordset></var></struct></data></wddxPacket> [Enable debugging by adding 'cfdebug' to your URL parameters to see more information]

After clicking Ok, I get this popup:

CFGRID: Response is empty [Enable debugging by adding 'cfdebug' to your URL parameters to see more information]

Now, my dilemma is that I'm on a shared hosting environment and apparently the debugging is turned off. I've tried adding "?cfdebug" to the query string to no avail.

Now, if I perform a cfdump on the CFC using cfinvoke, I show this:

struct
QUERY
query
ID LOGINID LOGINLEVEL LOGINNAME LOGINPW
1 3 jaime Admin Jaime XxXx
2 2 liz Admin Liz XxXx
3 1 steve Admin Steve Fister XxXx
4 [empty string] [empty string] [empty string] [empty string] [empty string]
5 [empty string] [empty string] [empty string] [empty string] [empty string]
TOTALROWCOUNT 3

I don't know if this is the problem, but it looks like there are two rows of empty strings and I have absolutely no idea where they are coming from! Checking the db table, there are indeed only 3 rows in the table.

Any ideas? Thanks!

Comment 17 by Steve posted on 3/19/2009 at 3:00 AM

Actually, the extra rows issue I have just figured out...they are there as placeholders from the setting of pageSize attribute in cfgrid. Anyhow, still not sure what's wrong with the JSON....

Thanks.

Steve

Comment 18 by Raymond Camden posted on 3/20/2009 at 6:43 AM

Notice the XML. It is WDDX. How are you doing your binding? You want to ensure returnformat=json is in the url.

Comment 19 by Meenakshi posted on 4/8/2009 at 7:57 PM

Ray, I am having a similar problem as steve mentioned earlier. I am using onRequestEnd and application.cfc.

When I load my grid page, I get a popup with this error:

Error parsing JSON response: {Total rows:25.... [Enable debugging by adding 'cfdebug' to your URL parameters to see more information]

After clicking Ok, I get this popup:

CFGRID: Response is empty [Enable debugging by adding 'cfdebug' to your URL parameters to see more information]

My CFGRID looks likes this:
<cfgrid format="html" name="employeeGrid" pagesize=11
stripeRows=true stripeRowColor="gray" bind="url:employeeService.cfc?method=getData&page={cfgridpage}&pagesize={cfgridpagesize}&gridsortcolumn={cfgridsortcolumn}&gridsortdirection={cfgridsortdirection}&returnFormat=json"
delete="yes" selectmode="edit"
onchange="cfc:employeeService.editData({cfgridaction},{cfgridrow},{cfgridchanged})">

<cfgridcolumn name="UserID" display=true header="Employee ID"/>
<cfgridcolumn name="FName" display=true header="First Name"/>
<cfgridcolumn name="LName" display=true header="Last Name"/>
<cfgridcolumn name="email" display=true header="Email" />

</cfgrid>

and my employeeService.cfc has this
<cfcomponent displayname="employeeService.cfc" output="false">

<cffunction name="getData" access="remote" output="false" >

<cfargument name="page">
<cfargument name="pageSize">
<cfargument name="gridsortcolumn" default="">
<cfargument name="gridsortdirection" default="">

<cfquery name="team" datasource="CMS">

SELECT UserID, FName, LName, email
FROM UserAccounts
<cfif gridsortcolumn neq "" or gridsortdirection neq "">
order by #gridsortcolumn# #gridsortdirection#
</cfif>

</cfquery>

<cfreturn QueryConvertForGrid(team, page, pageSize)>

</cffunction>
</cfcomponent>

Comment 20 by Raymond Camden posted on 4/8/2009 at 7:59 PM

What does your response look like? Use Firebug to see what you are getting.

Check to ensure CF Debugging is turned off.

Comment 21 by Raymond Camden posted on 4/8/2009 at 8:15 PM

Meenakshi pinged me via email with a screen shot of the error. THe error was a CF error. That means your server side code is throwing an error. The best thing to do is use Firebug's "Open request in new tab" option. This will let you run the page in a new tab and give you a better idea of the error CF is throwing.

Comment 22 by Tom McNeer posted on 4/22/2009 at 12:00 AM

I know this seems insane, because I swear I've used this workaround before. But it's not working now.

I have a simple "Hello World"-type Flex page for testing. It calls a CFC which returns the string "Hello."

When the CFC resides underneath the following Application.cfc, there's an error because the OnRequest method still runs.

Naturally, when I completely comment out the OnRequest method, it works fine. The code in OnRequestStart just isn't deleting OnRequest, for some reason.

Here's the entire Appliation.cfc:

<cfcomponent >
<cfset this.name = "fred" />

<cffunction name="onRequestStart">
<cfargument name = "targetPage" type="String" required="true" />
<!--- ensure CFC / Web Service / Flex Remoting calls are not intercepted --->
<cfif listlast(arguments.targetPage,".") is "cfc">
<cfset StructDelete(this, "onRequest") />
<cfset StructDelete(variables,"onRequest")/>
</cfif>
</cffunction>

<cffunction name="onRequest">
<cfargument name = "targetPage" type="String" required="true" />
<cfinclude template = "#Arguments.targetPage#" />
</cffunction>

</cfcomponent>

Comment 23 by Raymond Camden posted on 4/23/2009 at 12:37 AM

Have you tried logging in onRequestStart? It may help debug the issue.

Comment 24 by Jake posted on 5/5/2009 at 10:11 AM

Thank you Ray, this was driving me insane!

Comment 25 by Jon Taylor posted on 7/6/2009 at 4:12 AM

Wow .. Thankyou ..may the cf force go with you all .. this was driving me nuts.

As well as the dreaded "Response is empty" it can show in apache logs as an error: "(63)File name too long"

If you use the application.cfc heavily then wrap anything in the onRequestStart and onRequest in an if/else check for a cfc request and all is good (for me at least).

Comment 26 by FALCONSEYE posted on 7/16/2009 at 7:25 PM

Wow, This was about to drive me insane. Thanks Raymond...

Comment 27 by Ranga posted on 8/6/2009 at 4:10 AM

This was driving me nuts whole for the whole day.

The next day I saw something that drove me in commenting out all the functions of App.cfc and my ajax requests started working. I then figured out that onRequest was the one by uncommenting one by one.

Anyway this should help in uncommenting onRequest function as well.

Thanks to Ray and SC.

Comment 28 by Kelvin posted on 1/25/2010 at 1:03 AM

Wow! It's now 2010 and this problem still persists in CF. I have been studying the CF 8 volume 1 book. I practiced the examples on auto-suggest and cfgrid and everything went on smoothly. The next chapter introduced Application.cfc. I created an onRequestStart method which simply includes a header and an onRequestEnd method which includes a footer.

When I tried to show a friend the cool CF Ajax powered auto-suggest / cfgrid functionality, both of the examples I had earlier created didn't work:-(. After searching for a solution for a while, I decided to go back in time. I commented out everything in the Application.cfc and my auto-suggest and cfgrid examples worked again:-). In the end, I realised the problem was with the onRequestStart and onRequestEnd methods. That's how I finally landed in this forum.

The solution presented here does not solve my problem since I am not using any argument:

<cffunction name="onRequestStart" returntype="boolean" output="true" hint="Executed before each page request">
<!--- Display site header at top of every page --->
<cfinclude template="header.cfm" />

<cfreturn true />
</cffunction>

<cffunction name="onRequestEnd" returntype="void" output="true">

<cfinclude template="footer.cfm" />

</cffunction>

So how do I solve this?

Comment 29 by Raymond Camden posted on 1/25/2010 at 2:48 AM

Actually this problem is solved in ColdFusion 9.

Why does the solution not help you though? Your statement "I am not using any argument" doesn't make sense. I noticed your code sample showed a header and footer being included in every request. Is that the problem you are trying to solve? If so, that isn't a bug. You code says "Always include a header and footer." Therefore the fact that your Ajax requests break are entirely your fault. What you can do is sniff the page requested. That is always passed to onRequestStart and End. You haven't defined it, but it's passed. See the docs for these two methods. If you then examine the file requested you can simply NOT include the header/footer if the request ends in .cfc.

Also - I'd recommend not using onRequestStart/End for layout. I'd use a custom tag for that instead.

Comment 30 by Kelvin posted on 1/26/2010 at 1:12 AM

Hi Ray, sorry but I am still missing out something. (still on chapter 19 in the CF 8 book). I now understand that my Ajax requests break are being caused by the header and footer I am including on every page.

Actually here is the code of the auto-suggest page I am trying to render (it works when I comment out the header and footer in my Application.cfc):
<cfform>
<table align="center" style="background-color:green;">
<tr>
<th colspan="2">
<h3>Find an Art Name</h3>
</th>
</tr>
<tr>
<td>
Art Name:
</td>
<td>
<cfinput
type="text"
name="artName"
autoSuggest="cfc:components/artGateway.getArtForAutoSuggest({cfautosuggestvalue})"
size="50"
maxLength="100"
/>
</td>
</tr>
</table>
</cfform>

You said I can simply not include a header/footer if the request ends in .cfc. In this case the page I am requesting is a .cfm page or are you referring to the component being invoked by the auto-suggest control?

So it means I can't include a header on such a page simply by using the onRequestStart method?

I am still to study custom tags. When I get there, I will make use of a custom tag as you recommended.

Thanks

Comment 31 by Raymond Camden posted on 1/26/2010 at 3:31 AM

One quick nit - when you use CFC for binding like that, yo use dot notation. Replace the / with a .

I'm referring to the CFC you used for binding - because your onRequestStart/End runs all the time, it will try to wrap the CFC output with a header/footer as well.

Comment 32 by Kelvin posted on 1/26/2010 at 9:59 AM

Thanks Ray. Everything is now clear and I have been able to work around the problem as you earlier hinted.

Comment 33 by etothex23 posted on 3/24/2010 at 7:17 PM

When you say this is fixed in CF 9, what is fixed?
I have a header in the onRequestStart() and a footer in the onRequestEnd(). On one page I am building an ajax directory tool and it is a no go.
and i have a strange issue:
<!--- Include the requested ColdFusion template. --->
<cfif ListLast(TargetPage, "/") NEQ "DirectorySearch.cfm">
<!---<cfinclude template="#APPLICATION.header#" />--->
<cfelse>
<cfinclude template="#APPLICATION.header#" />
</cfif>
<!---Return out. --->
This shows the cfc and the header. which then kills it for the pages without the cfc

Comment 34 by Raymond Camden posted on 3/24/2010 at 7:22 PM

What's fixed in onRequest breaking the communication. Your code is working like you coded it. You said, before every request, include this template. Therefore, it's a bug in your code. :) You need to modify onRequestStart to look at the requested page. If it is a CFC, don't do the incude.

Comment 35 by DanGle neck posted on 6/1/2011 at 4:25 AM

Aww'sum Thanks for the answer to my problem. I've been banging my head trying to get ajaxproxy to work for over a week.

Comment 36 by DanGle neck posted on 6/1/2011 at 4:35 AM

This work-around also fixes the "?wsdl" problem I was having with cf8.

the URL: http://www.site.com/myCFC.c... was not displaying the xml.

Comment 37 by Jonathan posted on 7/30/2011 at 11:52 AM

Hi Ray, I need to include my header & footer on every page. How can I do that (as you suggested not to use cfInclude in onRequestStart) and still work with my CFCs

Comment 38 by Raymond Camden posted on 7/30/2011 at 5:34 PM

You could, in onRequestStart, sniff for the file extension of the request and if it is cfm, include your request. Ditto for onRequestEnd. I just don't like doing layout in App.cfc. I'd do it there "hard" way and wrap your CFMs with a layout custom tag. This means adding it to each CFM, but that gives you the most control. I promise if you do the "every cfm" approach in app.cfc, you will find a few CFMs where you _don't_ want it, and that means adding a complex CFIF to your logic.