gpt4 book ai didi

jquery - ColdFusion 9 JSON 解析错误?

转载 作者:行者123 更新时间:2023-12-01 04:00:17 24 4
gpt4 key购买 nike

我的代码版本在 ColdFusion 10 中工作正常,但在 ColdFusion 9 中我收到此错误:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 13714 of the JSON data

这是我的 cffunction,其中我的数据转换为 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}]}

此错误必须与 ColdFusion 版本 9/10 以及我的 JSON 数据的组织方式有关。我还是没有发现bug。如果有人看到我的代码 ID 损坏的地方,请告诉我。

最佳答案

我认为您的代码没有任何问题,只是 ColdFusion 中的 serializeJSON 有很多问题。

强制 CF9 将所有数字值视为字符串的一个技巧/技巧是将非数字文本附加到该值,并在客户端使用之前将其删除。我过去使用过 ASCII 控制字符 2,“文本开始”。我喜欢在其他文本上使用控制字符,因为我感到安全,我的用户不会故意使用它。

要将控制字符附加到建筑物编号,只需在 getBldg.number 之前添加 chr(2) & 即可。在客户端,您可以指示 jQuery 使用 dataFilter 属性从 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,"");
}
})

关于jquery - ColdFusion 9 JSON 解析错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46959005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com