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.

2) 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.

3) 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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments