Don't Double Encode Your JSON
A friend was having an issue with some Ajax stuff today and asked me to take a look. He had a Spry JSON dataset that called a CFC that he had built specifically to retrieve data for this dataset. Since he was on 8 and rolling his own method (instead of proxying another method) he correctly set the returnFormat of the CFC to 'JSON' which ensures the proper serialization on the resultset.
The issue he found was that his result set looked like something like this:
34.737Z\",
//and so on...
So the JSON string was being escaped. I took a look at his method and noticed that he was calling a remote service and had specified a return format of 'JSON' to the remote service. So when the function returned the string with the JSON returnFormat it was basically double encoding the string which gave him the goofy results. The fix was to specify a returnFormat of 'plain' so that the JSON string from the service was returned as is.
Just something to be mindful of with the popularity of JSON on the rise. Many public services are offering it as a return format so make sure you're only trying to serialize a coldfusion object/string when using the cffunction returnFormat.



There are no comments for this entry.
[Add Comment]