title: ColdFusion - How to access CFC return variables in jQuery?
body: <p>I am using ColdFusion 9 and jQuery.  I am using CFAJAXPROXY.</p>
  
  <p>I have am having trouble accessing the data returned to a jQuery object.</p>
  
  <p>This is part of my jQuery code:</p>
  
  <pre><code>var jro = new jsApp();
  NewUser = jro.addUser(NewEmail);
  </code></pre>
  
  <p>The method in the CFC is addUser. I pass in an Email address (NewEmail) and I get the new user's UserID back.</p>
  
  <p>This query in the CFC works just fine:</p>
  
  <pre><code>&lt;cfquery name="GetUserID"&gt;
  SELECT      MAX(UserID) as MaxID
  FROM        Users
  &lt;/cfquery&gt;
  </code></pre>
  
  <p>When I hard code a value like this, I do not have a problem:</p>
  
  <pre><code>&lt;cfset NewUser = 10&gt;
  &lt;cfreturn NewUser&gt;
  </code></pre>
  
  <p>When I try to use a dynamic variable like this, I get a JSON error:</p>
  
  <pre><code>&lt;cfset NewUser = GetUserID.MaxID&gt;
  &lt;cfreturn NewUser&gt;
  </code></pre>
  
  <p>This error seems to occur in the CFC. Hard coding the return variable works, but returning a single integer from a query does NOT work.  </p>
  
  <p>How do I return a single integer from a query? Why is this creating a problem?</p>
  
  <p>Here is the how the CFC is called.  </p>
  
  <pre><code>$("#AddUserSave").click(function() {
    NewEmail = $("#NewEmail").attr("value");
    var jro = new jsApp();
    UserID = jro.addUser(NewEmail);
    $("#UserDiv").load("GlobalAdmin/EditUser.cfm?UserID="+UserID);
  });
  </code></pre>
  
  <p>Again, the CFC refuses to return the UserID from the database, but gladly returns a hardcoded number.  </p>
  
  <p>Okay, in my CFC this works:</p>
  
  <pre><code>&lt;cfset NewUser = GetUserID.UserID&gt;
  &lt;cfset NewUser = 1&gt;
  &lt;cfreturn NewUser&gt;
  </code></pre>
  
  <p>But this does NOT work:</p>
  
  <pre><code>&lt;cfset NewUser = GetUserID.UserID&gt;
  &lt;cfreturn NewUser&gt;
  </code></pre>
  
  <p>Firebug says: </p>
  
  <pre><code>parseJSON throw new SyntaxError("parseJSON"); 
  cfajax.js (line 803)
  </code></pre>
  
  <p>Okay, when I access this method via the browser, it shows an integer being returned.</p>
  
  <pre><code> &lt;!--- GET MAX USER ---&gt;
   &lt;cffunction name="getMaxUser" access="remote" returnformat="plain" returntype="any"&gt;
      &lt;cfquery name="GetUserID"&gt;
          SELECT  MAX(UserID) AS MaxID
          FROM    Users
      &lt;/cfquery&gt;
      &lt;cfreturn createObject( 'java', 'java.lang.Integer' ).init( javaCast( 'int', GetUserID.MaxID ) ) /&gt;
  &lt;/cffunction&gt;
  </code></pre>
  
  <p>When I view the page source, the maxid is 1 (one / an integer).</p>
  
  <p>The CFC is being called with in JavaScript:</p>
  
  <pre><code>    var jro = new jsApp();
      jro.getMaxUser();
  </code></pre>
  
  <p>The exact error I am getting can be viewed on this screen shot:</p>
  
  <p><a href="http://evikjames.com/IMAGEs/parsejson.png" rel="nofollow">http://evikjames.com/IMAGEs/parsejson.png</a></p>
  
  <p>And here's an image of my CFCs.</p>
  
  <p><a href="http://evikjames.com/images/CFC.png" rel="nofollow">http://evikjames.com/images/CFC.png</a></p>
  
  <p>Here's an image of the URL created by JavaScript to access the method:</p>
  
  <p><a href="http://www.evikjames.com/images/error-2.png" rel="nofollow">http://www.evikjames.com/images/error-2.png</a></p>
  
  <p>+++++++++++++++++++++++++++++++++++++++++++++++++++++
  ANSWER</p>
  
  <p>The JavaScript needs to create a request that sets the return format to plain. I guess its default return type is JSON. The CFC chokes on the JSON that it own self has created.</p>
  
  <pre><code>var jro = new jsApp();
  jro.setReturnFormat('plain');
  UserID = jro.getMaxUser();
  </code></pre>
  
  <p>Hats off to Charlie for seeing this through.  I am forever grateful.</p>