I have version of the code that works fine in ColdFusion 10 but ColdFusion 9 I'm getting this error:
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 13714 of the JSON data
Here is my cffunction where my data is converted to JSON:
<cffunction name="getBuildings" access="remote" output="true" returnformat="JSON">
<cfset fnResults = structNew()>
<cfquery name="getBldg" datasource="myData">
SELECT
LTRIM(RTRIM(number)) AS bldgNum,
LTRIM(RTRIM(name)) AS bldgName
FROM Bldg WITH (NOLOCK)
ORDER BY name
</cfquery>
<cfset fnResults.recordcount = getBldg.recordcount>
<cfif getBldg.recordcount EQ 0>
<cfset fnResults.message = "No Buildings found.">
<cfelse>
<cfloop query="getBldg">
<cfset fnRecBldg[currentRow] = StructNew()>
<cfset fnRecBldg[currentRow].bldgName = URLEncodedFormat(getBldg.name)>
<cfset fnRecBldg[currentRow].bldgNumber = getBldg.number>
</cfloop>
<cfset fnResults.data = fnRecBldg>
</cfif>
<cfset fnResults.status = "200">
<cfreturn fnResults>
</cffunction>
Here is my JQUERY:
function getBldg(){
var dist = $('.chBldg');
$.ajax({
type: 'POST',
url: 'Application.cfc?method=getBuildings',
data: {},
dataType: 'json'
}).done(function(obj){
var numRecs = obj.RECORDCOUNT;
if(obj.STATUS == 200){
if(numRecs == 0){
dist.find("option:gt(0)").remove();
}else{
dist.find("option:gt(0)").remove();
for(var i=0; i < numRecs; i++){
var jsRec = obj.DATA[i];
dist.append($("<option />").val(jsRec.BLDGNUMBER).text(decodeURIComponent(jsRec.BLDGNAME) +' ('+ jsRec.BLDGNUMBER +')'));
}
}
}else{
}
}).fail(function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
});
}
}
Here is small example of rerurned data:
{"RECORDCOUNT":4,"STATUS":200,"DATA":[
{"BLDGNAME":"Fall%2DCasey","BLDGNUMBER":"0012"},
{"BLDGNAME":"Autmun","BLDGNUMBER":"0022"},
{"BLDGNAME":"Cedar","BLDGNUMBER":0201},
{"BLDGNAME":"Great%20Lake","BLDGNUMBER":1215}]}
This error must be related to ColdFusion version 9/10 and the way my JSON data is organized. I still didn't find the bug. If anyone see where my code id breaking please let me know.
I don't think there is anything wrong with your code, there is just lots wrong with serializeJSON
in ColdFusion.
One trick / hack to force CF9 to treat all numeric-ish values as strings is to append non numeric text to the value and strip it off before using it client side. I have used the ASCII control character 2, "Start of Text", in the past. I like using a control character over other text for I feel safe that my users wouldn't intentionally use it.
To append the control character to your building number, just add chr(2) &
before getBldg.number
. On your client side, you can instruct jQuery to remove the control characters from the JSON string with the dataFilter property.
$.ajax({
type: 'POST',
url: 'Application.cfc?method=getBuildings',
data: {},
dataType: 'json',
dataFilter: function(data, type){
//Remove all start of text characters
return data.replace(/\u0002/g,"");
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.