在cffunction中返回JSON并将其附加到图层会导致错误
-
24-10-2019 - |
题
我正在使用QTIP jQuery插件来生成动态工具提示。我在JS中遇到了一个错误,但我不确定其源是JSON还是JS。该工具提示调用以下函数:(对所有这些代码抱歉,但有必要)
<cffunction
name="fGameDetails"
access="remote"
returnType="any"
returnformat="JSON"
output="false"
hint="This grabs game details for the games.cfm page">
<!---Argument, which is the game ID--->
<cfargument
name="gameID"
type="numeric"
required="true"
hint="CFC will look for GameID and retrieve its details">
<!---Local var--->
<cfset var qGameDetails = "">
<!---Database query--->
<cfquery name="qGameDetails" datasource="#REQUEST.datasource#">
SELECT
titles.titleName AS tName,
titles.titleBrief AS tBrief,
games.gameID,
games.titleID,
games.releaseDate AS rDate,
genres.genreName AS gName,
platforms.platformAbbr AS pAbbr,
platforms.platformName AS pName,
creviews.cReviewScore AS rScore,
ratings.ratingName AS rName
FROM
games
Inner Join platforms ON platforms.platformID = games.platformID
Inner Join titles ON titles.titleID = games.titleID
Inner Join genres ON genres.genreID = games.genreID
Inner Join creviews ON games.gameID = creviews.gameID
Inner Join ratings ON ratings.ratingID = games.ratingID
WHERE
(games.gameID = #ARGUMENTS.gameID#);
</cfquery>
<cfreturn qGameDetails>
</cffunction>
此功能返回以下JSON:
{
"COLUMNS": [
"TNAME",
"TBRIEF",
"GAMEID",
"TITLEID",
"RDATE",
"GNAME",
"PABBR",
"PNAME",
"RSCORE",
"RNAME"
],
"DATA": [
[
"Dark Void",
"Ancient gods known as 'The Watchers,' once banished from our world by superhuman Adepts, have returned with a vengeance.",
154,
54,
"January, 19 2010 00:00:00",
"Action & Adventure",
"PS3",
"Playstation 3",
3.3,
"14 Anos"
]
]
}
我遇到的问题是,每次尝试将JSON附加到#catalog图层时,我都会收到一个语法错误,上面写着“缺少括号”。这是我正在使用的JavaScript:
$(document).ready(function()
{
$('#catalog a[href]').each(function()
{
$(this).qtip( {
content: {
url: '/gamezilla/resources/components/viewgames.cfc?method=fGameDetails',
data: { gameID: $(this).attr('href').match(/gameID=([0-9]+)$/)[1] },
method: 'get'
},
api: {
beforeContentUpdate: function(content) {
var json = eval('(' + content + ')');
content = $('<div />').append(
$('<h1 />', {
html: json.TNAME
}));
return content;
}
},
style: {
width: 300,
height: 300,
padding: 0,
name: 'light',
tip: {
corner: 'leftMiddle',
size: {
x: 40,
y : 40
}
}
},
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
}
});
});
});
有什么想法我要去哪里?我尝试了几天的许多事情,但找不到问题。
非常感谢!
解决方案 5
事实证明,这是ColdFusion调试器,导致Ajax变得很奇怪。我很久以前发现了这一点,但现在仅重新审视了这个问题。遇到此性质的错误时,第一件事应关闭CF调试器,以检查是否引起了问题。
其他提示
“位置”括号关闭后,JavaScript的底部有一个额外的支架。
做一个 console.log(arguments)
对于第一行 beforeContentUpdate
功能(在 eval
),以确保内容arg是您期望的?
看起来 beforeContentUpdate
不管用。所以我建议使用 onRender
打回来:
$(document).ready(function () {
$('#catalog a[href]').each(function () {
var link = $(this);
$(this).qtip({
content: 'loading...',
api: {
onRender: function () {
var self = this;
$.ajax({
url: '/gamezilla/resources/components/viewgames.cfc?method=fGameDetails',
dataType: 'json',
data: { gameID: link.attr('href').match(/gameID=([0-9]+)$/)[1] },
success: function (data) {
self.updateContent(data.DATA[0][0]);
}
});
}
},
style: {
width: 300,
height: 300,
padding: 0,
name: 'light',
tip: {
corner: 'leftMiddle',
size: {
x: 40,
y: 40
}
}
},
position: {
corner: {
target: 'rightMiddle',
tooltip: 'leftMiddle'
}
}
});
});
});
改变 var json = eval('(' + content + ')');
至 var json = eval(content);
和 html: json.TNAME
至 html: json.COLUMNS.TNAME
不隶属于 StackOverflow