jQuery.post() を使用して Coldfusion コンポーネント (cfc) を呼び出しています。URL で使用するために返される数値の整数または文字列表現が必要です。
{"PAGE":"My Page Title","ID":19382}
or
{"PAGE":"My Page Title","ID":"19382"}
代わりに、10 進数が返されます。
{"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>