views:

213

answers:

3

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>
A: 

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.

Jeff Price
No math being done. So do I surround the number with quotes on the server side to force it to a string over JSON?
Dan Sorensen
+2  A: 

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

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.

Henry
Thanks Henry. I thought I was going crazy. Glad I'm not the only one who ran across this. It also makes sense that I could use the number with the .0. Had not considered that.
Dan Sorensen
+4  A: 

It's a known SerializeJSON() bug/feature. See this answer for possible workaround.

Sergii
Gave you an up vote for the dead-on identification of the issue as well as the clarification that Ralio doesn't have the issue. However, Henry's solution is a little more manageable than prepending blank space and trimming later. (which I would absolutely do if the other solution didn't work.) Thanks!! :-)
Dan Sorensen