JsStringFormat () and apostrophe with JSON

So I am having an interesting problem with jsStringFormat() when trying to avoid special characters for JSON. I am using the jQuery datatables plugin and am making an AJAX call to Coldfusion.

What happens is that jsStringFormat () escapes the apostrophe character and puts \' in my JSON. According to the JSON specification, a single apostrophe does not need to be escaped, so it breaks.

Here is an example of my JSON return

 { "sEcho": 2, "iTotalRecords": 659, "iTotalDisplayRecords": 201, "aaData": [ ["516", "", "54d 7h 12m", "02- Revenue", "", "Assist in validating error in JCA provided Discount Commission report", "Received", "Work Request", "Jan 1, 2012"], ["616", "", "16d 7h 12m", "02- Revenue", "", "Order/Install new POS Terminal at Katie\ Workstation", "In Progress", "Work Request", "Oct 31, 2011"], ["617", "", "15d 7h 12m", "02- Revenue", "", "Replace #6081 POS Printer at Kim\ Desk", "Received", "Work Request", "Oct 31, 2011"] ] } 

You can clearly see the \' inserted in the description.

I really need to find a way to prevent jsStringFormat() from exiting the apostrophe.


UPDATE

So far, this code is trying to populate the aaData array. Right now, I only get commas, so I correctly know its cycle, but without filling in the data in the right place.

All this is based on datatables coldfusion datasource code http://www.datatables.net/development/server-side/coldfusion

 <cfcontent reset="Yes" /> <cfset aaData = [] /> <cfset datasetRecords = [] /> <cfloop query="qFiltered" startrow="#val(url.iDisplayStart+1)#" endrow="#val(url.iDisplayLength)#"> <cfif currentRow gt (url.iDisplayStart+1)>,</cfif> <cfloop list="#listColumns#" index="thisColumn"> <cfif thisColumn neq listFirst(listColumns)>,</cfif> <cfif thisColumn is "version"> <cfif version eq 0>"-" <cfelse><cfset datasetData["#version#"] /> </cfif> <cfelse><cfset datasetData[""] = qFiltered[thisColumn][qFiltered.currentRow] /> </cfif> <cfset ArrayAppend(datasetRecords, datasetData ) /> </cfloop> <cfset ArrayAppend(datasetRecords, aaData ) /> </cfloop> <cfset record = {} /> <cfset record["sEcho"] = val(url.sEcho) /> <cfset record["iTotalRecords"] = qCount.total /> <cfset record["iTotalDisplayRecords"] = qFiltered.recordCount /> <cfset record["aaData"] = aaData /> <cfoutput><cfdump var="#record#"></cfoutput> <cfoutput>#serializeJSON(record)#</cfoutput> 
+6
source share
2 answers

JSStringFormat designed to screen data for inclusion in JavaScript, not JSON. In JavaScript, one quotation mark is the character to be escaped.

SerializeJSON on the other hand, is actually intended for JSON output and conforms to the JSON specification.

+5
source

I ran into the same problem with datatables. what i did to fix this is create a jsstringformat string and then replace on the returned string to remove all instances of "c".

 <cfset thisColumnString = jsStringFormat(trim((qFiltered[thisColumn][qFiltered.currentRow])))> <cfset thisColumnString = replacenocase(thisColumnString,"\'","'","all")> "#thisColumnString#" 
+4
source

Source: https://habr.com/ru/post/898708/


All Articles