Interesting issue with ColdFusion's AJAX features and large strings

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:

  1. 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.

  1. 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.

  1. 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

Comment 1 by Daniel Sellers posted on 7/31/2008 at 11:40 PM

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.

Comment 2 by Raymond Camden posted on 7/31/2008 at 11:45 PM

Why bother though? You can tell a CFC to not JSON over the wire using returnFormat.

Comment 3 by Dominic posted on 8/1/2008 at 12:18 AM

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)

Comment 4 by Raymond Camden posted on 8/1/2008 at 1:00 AM

Ok now I'm confused.

Please grab

http://www.coldfusionjedi.c...

This what I'm using to test with.

Comment 5 by Dominic posted on 8/4/2008 at 9:10 PM

Sorry I was away from my computer all day friday. Looks like that's pointing to a different test file now.

Comment 6 by Raymond Camden posted on 8/5/2008 at 2:02 AM

Does this mean then my code works for you?

Comment 7 by Dominic posted on 8/5/2008 at 2:55 AM

No. The code in the file that is linked above is not related to the JSON issue.

Comment 8 by Raymond Camden posted on 8/5/2008 at 4:02 AM

Ugh - wrong file was uploaded. My machine is not available right now so I'll have to post it in the morning probably.

Comment 9 by Raymond Camden posted on 8/5/2008 at 4:14 AM

I recreated the code on anew machine, and it still works fine for me:

http://www.coldfusionjedi.c...

Comment 10 by Dominic posted on 8/5/2008 at 5:28 PM

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.

Comment 11 by Raymond Camden posted on 8/5/2008 at 5:54 PM

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.