8

I have a function that returns a JSON string, and ColdFusion 10 returns a slightly different value than ColdFusion 8.

In CF10, I get

{"ZIPCODE":90210,"PHONE":"(555) 382-6630","LAT":83,"DISTANCE":74,"NAME":"Pueblo, CO","ADDRESS":"6830 Meddley Drive","LONG":104}

but in CF8, I get this

{\"DISTANCE\":74,\"LAT\":83,\"ZIPCODE\":90210,\"NAME\":\"Pueblo, CO\",\"PHONE\":\"(555) 382-6630\",\"ADDRESS\":\"6830 Medley Drive\",\"LONG\":104.}

For the function, I have returnformat set to JSON and use serializeJSON() for the return value. I'm calling the function via jQuery's $.ajax method with dataType set to JSON

The backslashes from CF8 are causing errors in the javascript used to parse the data. Why is this happening, and is there a workaround?

RHPT
  • 2,362
  • 4
  • 28
  • 42
  • 7
    The CFC will automatically return it in JSON format, so calling `serializeJSON()` JSON-ifies the JSON. Which effectively escapes all the special characters with backslashes. Try it without calling `serializeJSON()`. – imthepitts Sep 23 '13 at 22:43
  • 2
    @imthepitts - (Edit) Yep, [no need to use both](http://www.raymondcamden.com/index.cfm/2008/6/3/Be-careful-with-returnFormat-and-JSON). (You could even omit both and just use the url parameter `?returnformat=json` to specify you want the result in json format). Anyway, you should write that up as an answer, btw. – Leigh Sep 23 '13 at 23:23
  • I wonder why this isn't a problem with CF10 ... – RHPT Sep 24 '13 at 01:58
  • 1
    Well if it does not "double-serialize" (as the code is instructing CF to do) then they obviously added some logic as a protection measure. CF must detect whether the result is already serialized, and if so it returns the result "as-is". – Leigh Sep 24 '13 at 02:26

1 Answers1

0

my guess would cf10 is automatically returning it in json format and cf8 isn't

snake
  • 632
  • 5
  • 11
  • 3
    No, based on the results CF10 is smart enough to recognize the code is unintentionally serializing the result twice (ie using both `serializeJSON()` **and** `returnformat="json"`) so it must ignore one of them. CF8 does not have that feature, so it serializes the result twice. – Leigh Oct 01 '13 at 23:00