0
votes

How do I pass a ColdFusion query object to my cfc function via jQuery Ajax? I am making an ajax call after I run a cfquery like so:

<cfoutput query="data">
    <script type="text/javascript">
       $.ajax({
          url: '/test.cfc',

          data:{
            q: #data#, //is this how i pass it?
            method: "getData"
          },
          success: function(data) {
           console.log(data);
          }
      });
   </script>
</cfoutput>

This seems to pass to my function, but I get a CF error that says:

Complex object types cannot be converted to simple values.

The console says "unexpected end of input". When I click on it, it highlights the q expression in my ajax call, with the value being:

<!-- " ---></TD></TD></TD></TH></TH></TH></TR></TR></TR></TABLE></TABLE></TABLE></A></ABBREV></ACRONYM></ADDRESS></APPLET></AU></B></BANNER></BIG></BLINK></BLOCKQUOTE></BQ>......

My cfc function is getting called and I have defined an argument with the same name.

<cfargument name="q" type="query" required="true" />

I guess my question is what am I missing or am I not passing the query properly?

2
Why are you trying to do this? Typically it is the other way around. - Leigh
<cfoutput query="data">...means, "loop over the records in this query and output this content". I don't think that's what you're tying to do. Can you update and tell us what you need to have happen in your presentation code? You would normally use Ajax to gather data from the browser and send it to the server. CFQUERY runs on the server, so you could just pass the results to whatever other CF function you want after its created, BEFORE you output data to the browser. - Adrian J. Moreno
Move whatever query you are passing to another method in your cfc. Then call that method from the one where you want to generate your json. - Dan Bracuk
(Edit) @surgiie - That should all be done on the server side. Create a function that runs the query and formats the results as needed. Then do an ajax call of that function. Plenty of existing threads on how to reformat query results, such as stackoverflow.com/questions/7814575/… stackoverflow.com/questions/14466754/… . - Leigh
FWIW, I have a GitHub repo that helps transform a ColdFusion query object to more standard JSON formats. github.com/iknowkungfoo/cfquery-to-json - Adrian J. Moreno

2 Answers

-1
votes

I believe you're looking for the SerializeJSON function.

<cfoutput query="data">
    <script type="text/javascript">
       $.ajax({
          url: '/test.cfc',

          data:{
            q: #SerializeJSON(data)#, //is this how i pass it?
            method: "getData"
          },
          success: function(data) {
           console.log(data);
          }
      });
   </script>
</cfoutput>

More: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-79fa.html

-1
votes

Perhaps something more manual is needed? I had to do this for things to work once...

<cfsavecontent variable="myOutput">{"QUERY":{"COLUMNS":["ID","BRANDID","CODE","ITEMNO","NAME","SIZE","UNIT","OUNCES","PRICE","DATETIME","USED"],"DATA":[<cfoutput query="parts" maxRows="#arguments.pageSize#">["#ID#","#BRANDID#","#CODE#","#ITEMNO#","#NAME#","#SIZE#","#UNIT#","#OUNCES#","#PRICE#","#datetime#",<cfif
        used gte 1>"Yes"<cfelse>"No"</cfif>]<cfif (currentRow neq recordCount) and (currentRow mod arguments.pageSize neq 0)>,</cfif></cfoutput>]},<cfoutput>"TOTALROWCOUNT":#result_count.totalRecords#}</cfoutput></cfsavecontent>