[英]ColdFusion 9 JSON parse error?
我有在 ColdFusion 10 中运行良好的代码版本,但在 ColdFusion 9 中我收到此错误:
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 13714 of the JSON data
这是我的数据转换为 JSON 的 cffunction:
<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>
这是我的 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);
});
}
}
这是重新生成的数据的小例子:
{"RECORDCOUNT":4,"STATUS":200,"DATA":[
{"BLDGNAME":"Fall%2DCasey","BLDGNUMBER":"0012"},
{"BLDGNAME":"Autmun","BLDGNUMBER":"0022"},
{"BLDGNAME":"Cedar","BLDGNUMBER":0201},
{"BLDGNAME":"Great%20Lake","BLDGNUMBER":1215}]}
此错误必须与 ColdFusion 版本 9/10 以及我的 JSON 数据的组织方式有关。 我还是没找到bug。 如果有人看到我的代码 ID 在哪里损坏,请告诉我。
我不认为你的代码有什么问题,ColdFusion 中的serializeJSON
有很多问题。
强制 CF9 将所有数字值视为字符串的一个技巧/技巧是将非数字文本附加到该值并在使用客户端之前将其剥离。 我过去使用过 ASCII 控制字符 2,“文本开始”。 我喜欢在其他文本上使用控制字符,因为我觉得我的用户不会故意使用它是安全的。
要将控制字符附加到您的建筑编号,只需在getBldg.number
之前添加chr(2) &
。 在您的客户端,您可以使用 dataFilter 属性指示 jQuery 从 JSON 字符串中删除控制字符。
$.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,"");
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.