ColdFusion Ajax leading zeros issue

A user posted a question to my forums that I thought would make for interesting blog matter. It concerns ColdFusion, binding, and results with leading zeros. I touched on this before (Interesting ColdFusion 8, AutoSuggest issue to watch for) so this may be something folks are aware of already. Here is some background. Imagine a CFC with this simple method:

<cffunction name="getKey" access="remote" returnType="string">
   <cfreturn "001234">
</cffunction>

Notice the result is a number, but with zeros in front. If you bind this to something on the front end:

<cfdiv bind="cfc:test.getkey()" />

You will only see 1234, not 001234. If you inspect the HTTP call with Firebug, you can see that the server returned 1234.0. ColdFusion took care of dropping the .0 at least, but this is not a desirable result. As mentioned in the earlier blog post, this seems to be the nature of the JSON serialization in ColdFusion. I can think of two simple ways around this.

One way would be to bind to a JavaScript function that then used cfajaxproxy to call the back end. Your back end cfc would need to prefix the result so the zeros aren't dropped. Like so:

<cffunction name="getKey2" access="remote" returnType="string">
   <cfreturn "pre" & getKey()>
</cffunction>

My problem with this solution is that your mucking with your model code. You shouldn't need to do that. Luckily there is another way to handle this. Don't forget that CFCs (in ColdFusion 8 at least) support a plain return format. Plain meaning "don't do squat to my result, just return it!". So if I change my cfdiv to:

<cfdiv bind="url:test.cfc?method=getkey&returnformat=plain" />

It works as expected now. I'm still using my CFC, but I've switch to the URL format. It's a bit more verbose, but at least my model CFC isn't changed.

Comments

To complicate this more, try using a 32 character string made up of numbers. (I only say 32 because that's where it's stung me recently. Other strings are probably impacted too)

IE: 12345678901234567890123456789012

This gets converted to scientific notation! I've never been able to get the format=plain recommendation to solve this issue but I've seen others who have. I decided to just prepend a space to the string and hope no one notices.
# Posted By Doug Hughes | 9/4/08 3:08 PM
This was the bug/fix I wasn't ever able to get working for you. Has anyone else been able to repro the issue you had? (I mean my fix to the issue not working for you - not the original issue.)
# Posted By Raymond Camden | 9/4/08 3:20 PM
Thanks for looking into this. I guess I need to convert my current bind statment for cfgrid bind="cfc:dir.services.subenditem.getPartPubData({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})" to use URL bind instead. So i used bind="url:/dir/services/subenditem.cfc?method=getPartPubData&returnformat=plain&page={cfgridpage}&pagesize={cfgridpagesize}&sortcolumn={cfgridsortcolumn}&sortdirection={cfgridsortdirection})" which seemed to work OK, but error occurs: Only simple and XML variables may be returned when the returnFormat is plain.

From Help file:

# plain: ensure that the return value is a type that ColdFusion can convert directly to a string, and return the string value without serialization. Valid types include all simple types, such as numbers, and XML objects. If the return value is a complex type, such as an array, or a binary value, ColdFusion generates an error. If you specify a returntype attribute, its value must be any, boolean, date, guid, numeric, string, uuid, variablename, or XML; otherwise, ColdFusion generates an error.

So I am still stuck.
# Posted By Giedrius | 9/4/08 6:35 PM
I guess the simplest solution is the best solution for me. I amde my select statement to be SELECT ' '+[PARTPUB_NUM] AS PARTPUB_NUM, ... which prepend a space. As a result leading zeroes are preserved, too.

On grid update I jusst trim the value, which trims the leading space. So in the circle it goes and it seems to work. I just wish we did not have to do things like that.
# Posted By Giedrius | 9/4/08 7:34 PM
Ah, in my example it was just a cfdiv, not a complex UI item. Sorry I missed that! But I'm glad you got it working at least.
# Posted By Raymond Camden | 9/4/08 7:47 PM