I was speaking with Doug Hughes earlier today about an interesting issue he ran into. He had a cfdiv bound to a CFC method like so:
<cfdiv bind="cfc:test.getdata()" />
Nothing too complex here, just a simple bind. His CFC though returned a large string that just so happened to look like a number as well. Here is an example:
<cffunction name="getdata" access="remote" returnType="string">
<cfreturn "12345678980123456789801234567898012345">
</cffunction>
So far so good. But when he viewed the page, this is what he saw:
1.2345678980123456e+37
Something in the process was treating the result as a real number and turning it into exponential form. We dug a big and discovered the following tidbits:
- If you open up Firebug you will clearly see that the conversion to number happens server side. It's the JSON conversion. You can see a simpler example of this with this code:
<cfset s = "12345678980123456789801234567898012345">
<cfset encoded = serializeJSON(s)>
<cfoutput>#encoded#</cfoutput>
Although this returns a slightly different result:
1.2345678980123456E37
So the thinking is that since CF is typeless, it's going to translate to JSON as best as it can. I tried to JavaCast thinking maybe I could force the issue, but no go.
- The solution I proposed was to simply use another bind type:
<cfdiv bind="url:test.cfc?method=getdata&returnformat=plain" />
Note the use of URL to point to the CFC. I have to provide more information (method and a return format), but for me, this worked perfectly.
- Unfortunately while this worked great for me, it didn't work for Doug. But personally I blame Doug. He has those shifty eyes ya know.
Seriously though - I think if you do run into this issue, using the URL format (with the returnFormat) should help. Basically if you see a result that doesn't make sense, you want to look and see if the JSON conversion is to blame.