ajaxcoldfusionmodel-glue

Ajax call in ModelGlue ColdFusion Application without rendering a view


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.


Solution

  • 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.