如何强制一个ColdFusion CFC输出数字数据通过JSON作为一个字符串? [英] How do I force a Coldfusion cfc to output numeric data over JSON as a string?

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

问题描述

我打电话使用jQuery.post ColdFusion组件(CFC)()。我需要一个整数或字符串,再以URL返回使用数量presentation。

  {页:我的页面标题,ID:19382}
要么
{PAGE:我的页面标题,ID:19382}
 

相反,我所得到的回复是一个十进制:

  {页:我的页面标题,ID:19382.0}
 

需要更新以下HTML:

 < A HREF =?page.cfm I​​D = 19382ID =pagelink>我的页面标题和LT; / A>
 

在概念上,我想有多个答案:

1),我可以使用jQuery抢小数点左边的数字。

2)我可能会迫使ColdFusion的要数派作为一个字符串。

3)我可能会产生整个链路服务器端只需更换整个链路标记HTML(而不是preferred的答案,但也许这是最好的)

有谁知道该怎么办1或2? 3好?

相应的JavaScript:(未优化)

  $(链接)。生活(点击,函数(){
    变量$的linkID,的serviceUrl;
    的serviceUrl =?mycfc.cfc方法= GETPAGE;
    $的linkID = $(本).attr(相对);

    $。员额(的serviceUrl,{的linkID:$的linkID},功能(结果){
        $('#pagelink)VAL(result.TITLE)。
        如果(result.FMKEY.length){
             //所需要的号码无.0在年底
             $('#pagelink')ATTR(的href)=page.cfm的id =?+ result.ID。
             $('#pagelink)文本(result.TITLE);
        }
    },JSON);
});
 

我的CFC:

 <成分输出=没有>
&所述; cfsetting showdebugoutput =无>
< cffunction名=GETPAGE访问=远程returnFormat =JSON输出=没有暗示=查找一个页面标题和ID>
    < cfargument名=的linkID类型=字符串要求=是>
    < CFSET VAR页= queryNew(ID,标题)>
    < CFSET VAR的结果= structNew()>
    < CFQUERY数据源=myDatasourceNAME =页面>
        SELECT TOP 1的ID,标题
        从页面
        WHERE的linkID =< cfqueryparam cfsqltype =cf_sql_integer值=#arguments.linkID#>
    < / CFQUERY>
    < CFIF page.recordcount>
        < CFSET result.id = page.id>
        < CFSET result.title = page.title>
    < / CFIF>
    < cfreturn结果>
< / cffunction>
< /成分>
 

在JS

解决方案

使用 parseInt()函数。 Kindda喜欢你的解决方案1,但是这是很容易,你的想法。

  $('#pagelink)ATTR(HREF)=page.cfm I​​D =?+ parseInt函数(result.ID,10);
 

CF连载像123的任意整数到123.0在默认情况下,但通常不会在无类型语言,像JS,或CF针对此事关系。

的默认行为 SerializeJSON()(遥控功能,使用什么样的)不能被覆盖,但如果你喜欢,你可以使用从WWW第三方JSON库之一.riaforge.org

P.S。即使您浏览到something.cfm?ID = 123.0, URL.id 是CF只是一个数值,EQ为123。虽然你的URL看起来有点怪异,如果是发布到CF卡,它仍然正常工作。

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>

解决方案

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

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作为一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆