Reputation: 1169
I am using Ajax with ModelGlue in a ColdFusion Application. I want to make an Ajax call to return a value. I do not want to render any view. I just want a database interaction and bring back a value.
My Ajax call:
new Ajax.Request(root+'test.testFunction',{
method: 'post',
parameters: {param1:paramval},
onSuccess: function(response){
alert(response.responseText);
var myresult = response.responseText;
}
});
my modelglue event :
<event-handler name="test.testFunction">
<broadcasts>
<message name="testFunction" />
</broadcasts>
</event-handler>
and my controller function:
<cffunction name="testFunction" returnType="any" output="true" >
<cfargument name="event" type="any" required="true">
<cfset justtest = 1>
<cfreturn justtest>
</cffunction>
I am using prototype as my ajax library.
When i alert the responseText i am getting null value. Is this bcoz i have not included the view part in the event handler? If i included the view part then i wud have to create a new page which i dont want to do. Is it possible to get just a server value by ajax call without rendering any view? I want to have myresult value as 1 according to the above scenario.
Pls help. Thnx for any help.
Upvotes: 1
Views: 789
Reputation: 53
WARNING: if Coldfusion 8, and have OnRequest() in application.cfc, there may be a related known cf8 bug. For a workaround, see http://www.coldfusionjedi.com/index.cfm/2008/3/19/Ask-a-Jedi-Ajaxbound-requests-and-onRequest
Upvotes: 0
Reputation: 632
Try using this at the end of your controller function:
<CFCONTENT TYPE="text" RESET="Yes"><CFOUTPUT>#serializeJSON(justTest)#
<cfset request.modelGlueSuppressDebugging = true />
<cfsetting showdebugoutput="false" /></CFOUTPUT><cfabort>
So like this:
<cffunction name="testFunction" returnType="any" output="true" >
<cfargument name="event" type="any" required="true">
<cfset justtest = 1>
<CFCONTENT TYPE="text" RESET="Yes"><CFOUTPUT>#serializeJSON(justTest)#
<cfset request.modelGlueSuppressDebugging = true />
<cfsetting showdebugoutput="false" /></CFOUTPUT><cfabort>
</cffunction>
This will keep your current view and return 'justTest' as a json.
If you are using firefox you should be able to see the reponse from the server.
Upvotes: 1
Reputation: 32915
In this situation, you should really be calling the remote proxy of your service, bypassing the MVC framework. :)
Oh, and don't forget you can use <cfajaxproxy>
if you're using CF8.
Upvotes: -1
Reputation: 19834
When you say you "just want to bring back a value" -- that's your "view". What you want to do is use a special view for your remote (ajax) event that just spits out the value. For example, if you want it to return JSON, you might do this:
Event Configuration:
<event-handler name="test.testFunction">
<broadcasts>
<message name="testFunction" />
</broadcasts>
<views>
<include name="body" template="renderJson.cfm" />
</views>
</event-handler>
Controller function:
<cffunction name="testFunction" returnType="any" output="true" >
<cfargument name="event" type="any" required="true">
<cfset event.setValue('justtest', 1) />
</cffunction>
renderJson.cfm:
<cfoutput>#serializeJson(event.getValue('justtest'))#</cfoutput>
If you're using Model-Glue 3, you can use the new Event Formats feature to piggy-back this ajax view on an existing event that does the same thing for a different view-format.
Upvotes: 3