[英]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.