Ask a Jedi: Ajax-bound requests and onRequest
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):
2<cfset StructDelete(this, "onRequest") />
3<cfset StructDelete(variables,"onRequest")/>
4</cfif>

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.
Is Adobe all consumed with Flex and Air now?
Thks.
Don
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.
<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.
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!
Thanks.
Steve
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>
Check to ensure CF Debugging is turned off.
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>
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).
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.
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?
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.
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
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.
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
the URL: http://www.site.com/myCFC.cfc?wsdl was not displaying the xml.