如何从javascript调用cfm页面上的cffunction? [英] How to call cffunction on the cfm page from javascript?

查看:68
本文介绍了如何从javascript调用cfm页面上的cffunction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在cfm页面上调用 cffunction ?我有 onClick 按钮,该按钮应调用同一页面上的 cffunction .我也尝试将 cfcomponent 放在函数周围,但出现此错误:

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>

这是我的JS函数:

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

我不确定这是否可能,我的当前代码没有触发 cffunction 中的警报.之所以尝试执行此操作,是因为此页面上有一个 cfquery ,并且当用户单击按钮然后进行一些操作时,我想从该查询中获取数据.如果有人可以告诉我是否可行,或者有更好的方法解决此问题,请告诉我.

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.

推荐答案

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

In the past, I've used cfajaxproxy to accomplish calling functions from a component (cfc) to javascript. (I don't do this any more because of different development paradigm, but this might be helpful to get you going.) First, create a cfc with your functions.

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

在模板.cfm文件中,您将使用

In your template, .cfm file, you would use cfajaxproxy to declare the component for us in your javascript.

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

然后,在同一模板中的javascript中,您将执行以下操作,并且将cfc函数用作jsClass中的方法.

Then, in the same template, in your javascript you would do the following and you will be able to use the cfc functions as methods from the 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开始的方式.

Hopefully, this provides some insight. There are many solutions out there, this being one. My current development pattern is to use Angular and do $http calls to CF components. And I roll my own JSON in the cffunctions. But this is how I started out with just plain JS and CF.

这篇关于如何从javascript调用cfm页面上的cffunction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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