如何强制 Coldfusion cfc 通过 JSON 将数字数据作为字符串输出?
我正在使用 jQuery.post() 调用 Coldfusion 组件 (cfc)。我需要返回的数字的整数或字符串表示形式,以便在 URL 中使用。
{"PAGE":"My Page Title","ID":19382}
or
{"PAGE":"My Page Title","ID":"19382"}
相反,我返回的是小数:
{"PAGE":"My Page Title","ID":19382.0}
需要更新以下 HTML:
<a href="page.cfm?id=19382" id="pagelink">My Page Title</a>
从概念上讲,我想有多个答案:
1)我可以使用 jQuery 来获取小数点左边的数字。
2) 我可以强制 Coldfusion 将数字作为字符串发送。
3)我可以生成整个链接服务器端,只需替换整个链接标签HTML(不是首选答案,但也许是最好的)
有谁知道如何做1或2? 3更好吗?
相关Javascript:(未优化)
$(".link").live('click', function () {
var $linkID, serviceUrl;
serviceUrl = "mycfc.cfc?method=getPage";
$linkID = $(this).attr("rel");
$.post(serviceUrl, { linkid: $linkID }, function (result) {
$('#pagelink').val(result.TITLE);
if (result.FMKEY.length) {
// NEED the ID number WITHOUT the .0 at the end
$('#pagelink').attr("href") = "page.cfm?id=" + result.ID;
$('#pagelink').text(result.TITLE);
}
}, "json");
});
我的CFC:
<component output="no">
<cfsetting showdebugoutput="no">
<cffunction name="getPage" access="remote" returnFormat="JSON" output="no" hint="Looks up a Page Title and ID">
<cfargument name="linkID" type="string" required="yes">
<cfset var page = queryNew("id,title")>
<cfset var result = structNew()>
<cfquery datasource="myDatasource" name="page">
SELECT TOP 1 id, title
FROM pages
WHERE linkID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.linkID#">
</cfquery>
<cfif page.recordcount>
<cfset result.id = page.id>
<cfset result.title = page.title>
</cfif>
<cfreturn result>
</cffunction>
</component>
I'm calling a Coldfusion component (cfc) using jQuery.post(). I need an integer or string representation of the number returned for use in a URL.
{"PAGE":"My Page Title","ID":19382}
or
{"PAGE":"My Page Title","ID":"19382"}
Instead what I get back is a decimal:
{"PAGE":"My Page Title","ID":19382.0}
Needed to update the following HTML:
<a href="page.cfm?id=19382" id="pagelink">My Page Title</a>
Conceptually, I suppose there are multiple answers:
1) I could use jQuery to grab the number left of the decimal point.
2) I could force Coldfusion to send the number as a string.
3) I could generate the whole link server side and just replace the whole link tag HTML (not the preferred answer, but maybe it is the best)
Does anyone know how to do 1 or 2? Is 3 better?
Relevant Javascript: (Not optimized)
$(".link").live('click', function () {
var $linkID, serviceUrl;
serviceUrl = "mycfc.cfc?method=getPage";
$linkID = $(this).attr("rel");
$.post(serviceUrl, { linkid: $linkID }, function (result) {
$('#pagelink').val(result.TITLE);
if (result.FMKEY.length) {
// NEED the ID number WITHOUT the .0 at the end
$('#pagelink').attr("href") = "page.cfm?id=" + result.ID;
$('#pagelink').text(result.TITLE);
}
}, "json");
});
My CFC:
<component output="no">
<cfsetting showdebugoutput="no">
<cffunction name="getPage" access="remote" returnFormat="JSON" output="no" hint="Looks up a Page Title and ID">
<cfargument name="linkID" type="string" required="yes">
<cfset var page = queryNew("id,title")>
<cfset var result = structNew()>
<cfquery datasource="myDatasource" name="page">
SELECT TOP 1 id, title
FROM pages
WHERE linkID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.linkID#">
</cfquery>
<cfif page.recordcount>
<cfset result.id = page.id>
<cfset result.title = page.title>
</cfif>
<cfreturn result>
</cffunction>
</component>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个已知的 SerializeJSON() 错误/功能。请参阅此答案了解可能的解决方法。
It's a known SerializeJSON() bug/feature. See this answer for possible workaround.
在 JS 中使用
parseInt()
。有点喜欢你的解决方案1,但这比你想象的要容易。默认情况下,CF 将任何整数(如 123)序列化为“123.0”,但通常在无类型语言(如 JS 或 CF)中这并不重要。
SerializeJSON() 的默认行为(远程函数使用的内容)无法被覆盖,但如果您愿意,您可以使用 www.riaforge.org 中的第 3 方 JSON 库之一
。即使您浏览到“something.cfm?id=123.0”,
URL.id
只是 CF 中的一个数值,EQ 为 123。虽然您的 URL 看起来有点奇怪,但如果它发布到CF,还是可以的。use
parseInt()
in JS. Kindda like your solution 1, but this is easier than you think.CF serializes any integer like 123 into "123.0" by default, but usually it doesn't matter in typeless language like JS, or CF for the matter.
The default behavior of
SerializeJSON()
(what remote function uses) cannot be overridden, but if you like, you can use one of the 3rd party JSON library from www.riaforge.orgp.s. Even if you browse to "something.cfm?id=123.0",
URL.id
is just a numeric value in CF that EQ to 123. Although your URL looks a little weird, if it is posting to CF, it'll still work.顺便说一句,当您宁愿使用解决方案 2)“我可以强制 Coldfusion 将数字作为字符串发送”时,例如,当使用需要像 dataTable 这样的字符串并且不想触及客户端代码的插件时......
仅仅尝试强制使用如下所示的数字或表示数字的字符串是行不通的:
但是添加前导空格将强制使用 JSON 字符串类型:
Hack hack hack,但可以派上用场。
As an aside, for when you'd rather use solution 2) "I could force Coldfusion to send the number as a string", for instance when using a plugin that expects strings like dataTable and not wanting to touch the client code...
Just trying to force the number or string representing a number like below will not work :
But adding a leading space will force the JSON string type :
Hack hack hack, but can come in handy.
从另一个角度来看,如果您也控制后端,您可以尝试使用 javaCast("int", someIntegerThatColdFusionThinksIsAFloat) 强制 ColdFusion 将数字序列化为整数。
Coming at it from another angle, if you control the back end too, you could try forcing ColdFusion to serialize the number as an integer, by using javaCast("int", someIntegerThatColdFusionThinksIsAFloat).
2) 我可以强制 Coldfusion 将数字作为字符串发送。
您是否对 id 进行了数学运算?可能不会。就您的 jquery 而言,这是一个仅包含数字而不包含整数的字符串。就这样对待它。
2) I could force Coldfusion to send the number as a string.
Are you doing any math with the id? Probably not. As far as your jquery is concerned, this is a string that contains only numbers, not an integer. Treat it as such.