2

I was wondering if I can call cffunction on my cfm page? I have onClick button that should make a call to the cffunction that is on the same page. Also I have tried to put cfcomponent around my function but I was getting this error:

Invalid CFML construct found on line 94 at column 1.
ColdFusion was looking at the following text:

<
The CFML compiler was processing:
< marks the beginning of a ColdFusion tag.Did you mean LT or LTE?

So far I have this:

<cffunction name="getRecords" access="remote">
    <script>
        alert('test');
    </script>
</cffunction>

here is my JS Function:

function getRecs(){
    try{
        location.href = 'myCFMpage.cfm?method=getRecords';
    }catch(err){
        alert('Error')
    }   
}

I'm not sure if this even possible, my current code did not trigger alert in cffunction. Reason why I'm trying to do this because I have a cfquery on this page and I want to grab the data from that query when user click on the button and then to do some manipulation. If anyone can tell me if this is possible or is there any better way to approach this problem please let me know.

4

1 回答 1

7

过去,我使用 cfajaxproxy 来完成从组件 (cfc) 到 javascript 的调用功能。(由于开发范式不同,我不再这样做了,但这可能对您有所帮助。)首先,使用您的函数创建一个 cfc。

<cfcomponent>
    <cffunction name="getRecords" access="remote" returntype="string">
        <cfquery name="someQuery" datasource="someDataSource">
            select * from records
        </cfquery>
        <cfreturn serializeJSON(someQuery,'struct')> 
    </cffunction>
</cfcomponent>

在您的模板 .cfm 文件中,您将使用cfajaxproxy在您的 javascript 中为我们声明组件。

<cfajaxproxy cfc="yourComponent" jsclassname="jsClass">

然后,在同一个模板中,在您的 javascript 中,您将执行以下操作,您将能够使用 cfc 函数作为来自 jsClass 的方法。

<script type="text/javascript">
    var _myFuncs = new jsClass()

    function buttonClicked() {
        var _records = JSON.parse(_myFuncs.getRecords());
    }

</script> 

希望这能提供一些见解。有很多解决方案,这是一个。我目前的开发模式是使用 Angular 并对 CF 组件进行 $http 调用。我在 cffunctions 中滚动我自己的 JSON。但这就是我开始使用普通 JS 和 CF 的方式。

于 2016-11-03T17:33:19.627 回答