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.
Archived Comments
Would it be possible (though a little less clean) to have a .cfm page that called the cfc and displayed the result and then bind to that .cfm instead of the cfc? Haven't tried this yet... but just a thought. Shouldn't have the json conversion then.
May not work for Doug's application though.
Why bother though? You can tell a CFC to not JSON over the wire using returnFormat.
I ran into this problem a while ago. For what it's worth, it will also strip leading zeroes. Unfortunately the returnformat trick doesn't work for me either. (version 8,0,1,195765, 64bit)
Ok now I'm confused.
Please grab
http://www.coldfusionjedi.c...
This what I'm using to test with.
Sorry I was away from my computer all day friday. Looks like that's pointing to a different test file now.
Does this mean then my code works for you?
No. The code in the file that is linked above is not related to the JSON issue.
Ugh - wrong file was uploaded. My machine is not available right now so I'll have to post it in the morning probably.
I recreated the code on anew machine, and it still works fine for me:
http://www.coldfusionjedi.c...
Ah okay this makes more sense. I was thrown off by the code above
<cfset s = "12345678980123456789801234567898012345">
<cfset encoded = serializeJSON(s)>
<cfoutput>#encoded#</cfoutput>
I thought that's what your cfc was doing. Your test.cfc works as expected for me.
Ah yes. I had wanted to compare the display of similar code but not over Ajax. So my test file had 1 call to Ajax, another call with the url format, then the inline code.