How to force Coldfusion cfc to output numeric data via JSON as a string?

I am calling the Coldfusion component (cfc) using jQuery.post (). I need an integer or string representation of the number returned for use in the url.

{"PAGE":"My Page Title","ID":19382} or {"PAGE":"My Page Title","ID":"19382"} 

Instead of what I get, this is a decimal number:

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

The following HTML needs to be updated:

 <a href="page.cfm?id=19382" id="pagelink">My Page Title</a> 

It is clear that there are several answers:

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

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

3) I could generate the whole side of the link server and just replace the whole HTML link tag (not the preferred answer, but maybe this is the best)

Does anyone know how to make 1 or 2? Is it improved 3?

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> 
+4
source share
5 answers

use parseInt() in JS. Kindda likes your solution 1, but it's easier than you think.

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

CF serializes any integer 123 to "123.0" by default, but usually it doesn't matter in an unprincipled language like JS or CF for this question.

The default behavior of SerializeJSON() (using a remote function) cannot be overridden, but if you like, you can use one of the third-party JSON libraries from www.riaforge.org

ps Even if you go to "something.cfm? Id = 123.0", URL.id is just a numeric value in CF that EQ is up to 123. Although your URL looks a little strange if it sends CF, it will still work.

+4
source

This is a known bug / SerializeJSON () function. See this answer for a possible workaround.

+8
source

Alternatively, if you prefer to use solution 2) "I can force Coldfusion to send the number as a string", for example, when using a plugin that expects a string like dataTable and does not want to touch the client code ...

Just trying to force a number or a string representing a number like below will not work:

 Test["caseSensitiveName"] = "#numericString#"; 

But adding a leading space forces the JSON string type:

 Test["caseSensitiveName"] = " #numericString#"; 

Hack hack hack, but may come in handy.

+1
source

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

Do you do math with id? Probably no. As for your jquery, this is a string that contains only numbers, not an integer. Consider this as such.

0
source

Approaching this from a different angle, if you also control the back end, you can try to get ColdFusion to serialize the number as an integer using javaCast ("int", someIntegerThatColdFusionThinksIsAFloat).

0
source

Source: https://habr.com/ru/post/1304597/


All Articles