How can I use a ColdFusion function that is on the same page as a script?

I normally call a cffunction with Ajax in ColdFusion like the below example. I want to do the exact same thing, but instead of having the cffunction on a separate .cfc page, I want to have it right on the .cfm page that I am calling it from. I want the <script> and CFFunction on the same page.

Can someone help?

<cfajaxproxy cfc="/mySite/myDir/myCFC" jsclassname="jsobj">

<script>
	function myFunction(){
	var somethingOne= 'somethingOne';
	var somethingElse = 'somethingElse';
	var cfcAsAjax = new jsobj();
	cfcAsAjax.myCFFunction(somethingOne,somethingElse);
	}
</script>

On the .CFC page, I just have a normal CFFunction:

<cffunction name="myCFFunction" output="false" access="remote">
	<cfargument required="true" type="numeric" name="somethingOne" />
	<cfargument required="true" type="String" name="SomethingElse" />
..........

Open in new window

earwig75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
Any specific reason you can't use a separate cfc?  AFAIK, CFAjaxProxy only works on cfc's, so it won't work with .cfm pages.   Technically I suppose you could do something similar with jquery, but ... I wouldn't advise it. You'd have to add conditionals to the page to do one thing when it was an ajax request, and something else the rest of the time, which is very messy and error prone IMO...
earwig75Author Commented:
There's no reason, I just wanted to keep it simple with 1 less page. I like to use .cfc files when I plan on reusing code. Thank you.
_agx_Commented:
Well, I don't think single page approach is possible in this specific case. At least not with cfajaxproxy.

In this one case, 2 files is simpler IMO :) At least from a maintainability perspective.  Forcing a single script to serve dual purposes ie return text/html AND ajax/wddx/xml will make it a lot more complex and error prone.  Ajax stuff can be incredibly picky about things like extra white space, etc... and those are much harder to control from a .cfm script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_agx_Commented:
I was curious if using jquery might be simpler than I was thinking, so I did a quick comparison of the CFAjaxProxy + 2 scripts versus jquery + 1 script.  It quickly came back to me why doing ajax calls to .cfm scripts is so painful ;-)  

What you have already, ie CFAjaxProxy + 2 scripts is much cleaner. No contest:


CFAjaxProxy + 2 scripts

Script1.cfm
<cfajaxproxy cfc="myCFC" jsclassname="jsobj">
<script type="text/javascript">
	function myFunction(){
		var somethingOne = 1;
		var somethingElse = 'somethingElse';
		var cfcAsAjax = new jsobj();
		var response = cfcAsAjax.myCFFunction(somethingOne,somethingElse);
		alert("RESPONSE \n"+ response.MESSAGE);
	}
</script>

<form>
	<input type="button" value="Test" onClick="myFunction()">
</form>

Open in new window


MyCFC.cfc
<cfcomponent>
	<cffunction name="myCFFunction" output="false" access="remote" returntype="struct">
		<cfargument required="true" type="numeric" name="somethingOne" />
		<cfargument required="true" type="String" name="SomethingElse" />
		<cfset Local.result.message = "somethingOne = "& arguments.somethingOne &" somethingElse="& arguments.SomethingElse />
		<cfreturn Local.result />
	</cffunction>
</cfcomponent>

Open in new window



Jquery + single script (yikes...)
<!--- This code MUST be at the top of the page --->
<!--- Suppress extra white space to avoid ajax problems.  --->
<!--- Debugging must be off or it will break the request --->
<cfsetting enablecfoutputonly="true" showdebugoutput="false">
<cfif structKeyExists(FORM, "method") AND FORM.method eq "myFunction">
	<!--- returning a response for demo purposes --->
	<cfset response = {message = myCFFunction(argumentCollection=FORM)}>
	<cfcontent type="application/json" reset="true">
	<cfoutput>#serializeJSON(response)#</cfoutput>
	<!--- must abort or the ajax request will break <cfabort>
</cfif>

<cfsetting enablecfoutputonly="false">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
	function myFunction(){
		var firstVar = 1;
		var secondVar = 'somethingElse';
		$.ajax({
		    // update path to script
			url: 'allInOneScript.cfm'
			, method: 'POST'
			, data: { somethingOne: firstVar, SomethingElse: secondVar, method : 'myFunction' }
			}).done(function(response){
			    // do something on success
				alert("RESPONSE \n"+ response.MESSAGE);
			}).fail(function (jqXHR, textStatus) {
			    // do something on failure
				alert("Failure ");
				console.log(jqXHR);
			});
  } 
</script>

<form>
	<input type="button" value="Test" onClick="myFunction()">
</form>

<!--- technically functions can be defined anywhere in the script. placing outside ajax code for clarity --->
<cffunction name="myCFFunction" output="false" access="remote">
	<cfargument required="true" type="numeric" name="somethingOne" />
	<cfargument required="true" type="String" name="SomethingElse" />
	<cfreturn "Values received: somethingOne = "& arguments.somethingOne &" somethingElse="& arguments.SomethingElse />
</cffunction>

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.