如何强制 Coldfusion cfc 将 JSON 上的数字数据作为字

How do I force a Coldfusion cfc to output numeric data over JSON as a string?(如何强制 Coldfusion cfc 将 JSON 上的数字数据作为字符串输出?)

本文介绍了如何强制 Coldfusion cfc 将 JSON 上的数字数据作为字符串输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery.post() 调用 Coldfusion 组件 (cfc).我需要在 URL 中使用返回的数字的整数或字符串表示形式.

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"}

我得到的是一个小数:

{"PAGE":"My Page Title","ID":19382.0}

需要更新以下 HTML:

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) 我可以使用 jQuery 来抓取小数点左边的数字.

1) I could use jQuery to grab the number left of the decimal point.

2) 我可以强制 Coldfusion 将数字作为字符串发送.

2) I could force Coldfusion to send the number as a string.

3)我可以生成整个链接服务器端,只需替换整个链接标记 HTML(不是首选答案,但也许是最好的)

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)

有人知道怎么做 1 或 2 吗?3更好吗?

Does anyone know how to do 1 or 2? Is 3 better?

相关的Javascript:(未优化)

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");
});

我的 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>

推荐答案

在 JS 中使用 parseInt().有点喜欢您的解决方案 1,但这比您想象的要容易.

use parseInt() in JS. Kindda like your solution 1, but this is easier than you think.

$('#pagelink').attr("href") = "page.cfm?id=" + parseInt(result.ID, 10);

CF 默认将任何整数如 123 序列化为123.0",但通常在无类型语言如 JS 或 CF 中无关紧要.

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.

SerializeJSON() 的默认行为(远程函数使用什么)不能被覆盖,但如果您愿意,可以使用 www.riaforge.org 中的第 3 方 JSON 库之一

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.org

附言即使您浏览到something.cfm?id=123.0",URL.id 也只是 CF 中的一个数值,等于 123.虽然您的 URL 看起来有点奇怪,但如果它发布到CF,它仍然可以工作.

p.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.

这篇关于如何强制 Coldfusion cfc 将 JSON 上的数字数据作为字符串输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何强制 Coldfusion cfc 将 JSON 上的数字数据作为字

基础教程推荐