我的代码版本在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 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}]}这个错误一定与JSONVersion9/10和我的ColdFusion数据的组织方式有关。我还是没找到虫子。如果有人看到我的代码在哪里出错,请让我知道。
发布于 2017-10-27 04:12:26
我不认为你的代码有什么问题,只是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,"");
}
})https://stackoverflow.com/questions/46959005
复制相似问题