I simplified this code for ease of explanation.
I have a cfm page where the user clicks on a table row and gets the id. I want to send this identifier to CFC, run a request there and return the results to the cfm page.
Here is what jQuery looks like.
$.ajax({ url: "test.cfc?method=testFunction", data: {ID:123456}, success: function(response) { $("#div1").html(response); } });
and this is what cfc looks like.
<cfcomponent> <cffunction name="testFunction" access="remote" returnType="query" returnFormat="JSON"> <cfquery name="testQuery" datasource="x"> Select ID, NAME From Table Where ID = '#url.ID#' </cfquery> <cfreturn testQuery> </cffunction> </cfcomponent>
EDIT - CFC ALTERNATIVE METHOD
<cffunction name="testFunction" access="remote"> <cfquery name="testQuery" datasource="x"> Select ID, NAME From Table Where ID = '#url.ID#' </cfquery> <cfset response = [] /> <cfoutput query="testQuery"> <cfset obj = { "ID" = ID, "NAME" = NAME } /> <cfset arrayAppend(response, obj) /> </cfoutput> <cfprocessingdirective suppresswhitespace="Yes"> <cfoutput> #serializeJSON(response)# </cfoutput> </cfprocessingdirective> <cfsetting enablecfoutputonly="No" showdebugoutput="No"> </cffunction>
As the success function in the ajax call from above shows, div1 will be populated with a JSON response that looks like this.
{"COLUMNS":["ID","NAME"],"DATA":[[123456,"John"]]}
EDIT - ALTERNATIVE RESPONSE
[{"ID":123456,"NAME":"John"}]
Next, I want to be able to use and output data from this JSON response somewhere on my page. How can i do this? I find it difficult to understand the analysis of this data. My main task is to get this data from the array format so that I can output it to the form fields on my page.