So I am having an interesting problem with jsStringFormat()
when trying to escape special characters for JSON. I am using the jQuery datatables plugin and doing an AJAX call to Coldfusion.
What appears to be happening is that jsStringFormat() is escaping the apostrophe character and putting \'
in my JSON. According to JSON spec, the single apostrophe doesn't need escaping, thus it breaks.
Here is a sample 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\'s Workstation", "In Progress", "Work Request", "Oct 31, 2011"],
["617", "", "15d 7h 12m", "02- Revenue", "", "Replace #6081 POS Printer at Kim\'s Desk", "Received", "Work Request", "Oct 31, 2011"]
]
}
You can clearly see the \'
inserted in the descriptions.
I really need to find a way to prevent jsStringFormat()
from escaping the apostrophe.
UPDATE
So far, have this code for attempting to populate the aaData array. Right now I am getting nothing but commas so I k开发者_如何学运维now its looping properly, but not populating the data in the right places.
All of this is based off of 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>
JSStringFormat
is designed for escaping data for inclusion in JavaScript, not JSON. In JavaScript, a single quote is a character that needs to be escaped.
On the other hand, SerializeJSON
is actually meant to output JSON, and complies with the JSON spec.
i ran into the same issue with datatables. what i did to fix it was create the jsstringformat string and then do a replace on the returned string to remove all instances of \' with '.
<cfset thisColumnString = jsStringFormat(trim((qFiltered[thisColumn][qFiltered.currentRow])))>
<cfset thisColumnString = replacenocase(thisColumnString,"\'","'","all")>
"#thisColumnString#"
精彩评论