using application.cfc and webservices / ajax
I'm not going to blog about how to use the application.cfc file, because there are a lot of experts out there that already has. And Ray Camden has posted a CF8 "template" for application.cfc. But i haven't seen anyone answer the question about using onRequest method within the application.cfc while using ajax / webservices in the same directory.
As most of you have already seen, the normal "template" for onRequest is as follows:
But since ajax and webservices are actually CFC methodology this included code will break it.
Now I am not sure if the following cures all scenarios, but it gave me the ability to do webservices / ajax.
Notice two things:
As most of you have already seen, the normal "template" for onRequest is as follows:
<cffunction name="onRequest" returnType="void">
<cfargument name="thePage" type="string" required="true">
<cfinclude template="#arguments.thePage#">
</cffunction>
But since ajax and webservices are actually CFC methodology this included code will break it.
Now I am not sure if the following cures all scenarios, but it gave me the ability to do webservices / ajax.
<cffunction name="onRequest">
<cfargument name = "targetPage" type="String" required=true/>
<cfif Right(arguments.targetPage,3) EQ "cfc">
<cfparam name="url.method" default="">
<cfset variables.component = Left(arguments.targetPage,Len(arguments.targetPage)-4)>
<cfset variables.component = Replace(variables.component,"/",".","ALL")>
<cfinvoke method="#url.method#" component="admissions.#variables.component#" returnvariable="methodOutput">
<cfif IsDefined("methodOutput")>
<cfwddx input="#methodOutput#" output="wddxOutput" action="cfml2js" toplevelvariable="rtnData">
<cfoutput>#wddxOutput#</cfoutput>
</cfif>
<cfelse>
<cfinclude template="/admissions/#Arguments.targetPage#">
</cfif>
</cffunction>
Notice two things:
- The onRequest page now checks to see the extension of the file being requested (CFC or CFM). If it is a CFC it invokes a method instead of including it.
- The CFC section if the function 'RETURNS' it output, using the cfreturn tag, then we need to convert it to WDDX. This is the standard CF methodology.
Actually in CF9 they finally fixed this. It should bypass onRequest for any CFC request. There is a new method, onCFCRequest, that can be used instead.
ReplyDelete