I am using ColdFusion 9 and jQuery. I am using CFAJAXPROXY.
I have am having trouble accessing the data returned to a jQuery object.
This is part of my jQuery code:
var jro = new jsApp();
NewUser = jro.addUser(NewEmail);
The method in the CFC is addUser. I pass in an Email address (NewEmail) and I get the new user’s UserID back.
This query in the CFC works just fine:
<cfquery name="GetUserID">
SELECT MAX(UserID) as MaxID
FROM Users
</cfquery>
When I hard code a value like this, I do not have a problem:
<cfset NewUser = 10>
<cfreturn NewUser>
When I try to use a dynamic variable like this, I get a JSON error:
<cfset NewUser = GetUserID.MaxID>
<cfreturn NewUser>
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.
How do I return a single integer from a query? Why is this creating a problem?
Here is the how the CFC is called.
$("#AddUserSave").click(function() {
NewEmail = $("#NewEmail").attr("value");
var jro = new jsApp();
UserID = jro.addUser(NewEmail);
$("#UserDiv").load("GlobalAdmin/EditUser.cfm?UserID="+UserID);
});
Again, the CFC refuses to return the UserID from the database, but gladly returns a hardcoded number.
Okay, in my CFC this works:
<cfset NewUser = GetUserID.UserID>
<cfset NewUser = 1>
<cfreturn NewUser>
But this does NOT work:
<cfset NewUser = GetUserID.UserID>
<cfreturn NewUser>
Firebug says:
parseJSON throw new SyntaxError("parseJSON");
cfajax.js (line 803)
Okay, when I access this method via the browser, it shows an integer being returned.
<!--- GET MAX USER --->
<cffunction name="getMaxUser" access="remote" returnformat="plain" returntype="any">
<cfquery name="GetUserID">
SELECT MAX(UserID) AS MaxID
FROM Users
</cfquery>
<cfreturn createObject( 'java', 'java.lang.Integer' ).init( javaCast( 'int', GetUserID.MaxID ) ) />
</cffunction>
When I view the page source, the maxid is 1 (one / an integer).
The CFC is being called with in JavaScript:
var jro = new jsApp();
jro.getMaxUser();
The exact error I am getting can be viewed on this screen shot:
http://evikjames.com/IMAGEs/parsejson.png
And here’s an image of my CFCs.
http://evikjames.com/images/CFC.png
Here’s an image of the URL created by JavaScript to access the method:
http://www.evikjames.com/images/error-2.png
+++++++++++++++++++++++++++++++++++++++++++++++++++++
ANSWER
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.
var jro = new jsApp();
jro.setReturnFormat('plain');
UserID = jro.getMaxUser();
Hats off to Charlie for seeing this through. I am forever grateful.
As per our chat, you need your remotely accessible method to return an integer, not a float.
With credit to http://www.threesources.com/archives/009007.html, you need to do the following:
1) specify returnFormat = “plain” (which I believe you’ve done)
2)
<cfreturn createObject( 'java', 'java.lang.Integer' ).init( javaCast( 'int', GetUserID.MaxID ) ) />As per the site linked to above, the reason is, “ColdFusion’s Numeric type maps to a double in the automatically generated RPC WSDL”.
That site lists 5 steps that need to be taken, but I was able to successfully return an integer by just doing the 2 steps listed above.