简体   繁体   中英

Remote Server Call to Coldfusion Function from Javascript/jQuery

I'm new to ColdFusion but knowledgeable in JavaScript/jQuery. I've spent much time researching this question and think it must be simple, but couldn't come up with a solution.

I'd like to invoke a server-side call to a ColdFusion function from within my Javascript function. So that when the user clicks "Submit", and all requirements are met, an email is sent automatically through my ColdFusion mail server. No luck so far. This is what I have:

JavaScript:

function callback(value) 
{
    if (value)
    {
        if (newShowing) //checks validity of email address again before submitting
        {
            var email = $("#contactEmail").val();
        }
        else 
        {
            var email = $("#contactEmail2").val();
        } 

        var isEmail = validateEmail2(email); 
        if (isEmail == true)
        {

           //invoke coldfusion sendEmail function
           $.get('/department/indexWebReq.cfm?method=sendEmail'); 
           alert("Submitted Request!");
           $("#webRequest-form").dialog("close");
        }

    }
}/

ColdFusion:

<cffunction name="sendEmail" returntype = "void">
    <cfmail to="email@email.edu"
       from="email@gmail.com"
       subject="Welcome to Bedrock"
       type="text">
        Dear User,

        We, here at Bedrock, would like to thank you for joining.

        Best wishes
        Barney
    </cfmail>
    <cfoutput>
       <p>Thank you, UserName, for registering.
        We have just sent you an email.</p>
    </cfoutput>
</cffunction>

The only time I've done something like this was when I wanted to see if I could. I had this jquery code:

<script type="text/javascript" charset="utf-8" src="/JS/lib/jquery-1.8.0.min.js"></script>
<script>

$(document).ready(function(){
alert("before");

$.ajax({

url: "something.cfc?method=abc&returnFormat=JSON",
global: false,
type: "POST",
//data: {username:name,password:pass},
dataType: "json",
async:false,
success: function(msg){
alert(msg);
}
});
alert("after");
}); 

And this CF Code

<cfcomponent>
<cffunction name="abc" access="remote" returntype="string">
<cfreturn "hello">
</cffunction>
</cfcomponent>

The code executes successfully. It is meant as a starting point for your situation, not a final answer.

Note that the function has its access attribute set to remote. That makes it available to javascript, among other things.

You can use cfajaxproxy to make a Remote Server Call to Coldfusion Function from Javascript/jQuery in a cfm file.

You can add the following tag to the page you want to access CF functions via JS
<cfajaxproxy cfc="name_of_the_component" jsclassname="desired_js_class_name">

After adding the above tag in the cfm page you can write-

<script language="javascript">
        var instance = new desired_js_class_name();
        var result = instance.cf_function_to_call();
</script>

Note: This will work as long as you are using JS in a CFM page .

You'll need to have your ColdFusion code in a CFC, and the method marked as access="remote" . I've taken a shot at it based on what you posted, adding a few dynamic arguments for example sake, but I haven't tested this fully because of the email implications.

<cfcomponent displayname="mycfc">
    <cffunction name="sendEmail" returntype="void" access="remote">
        <cfargument name="to" type="string" required="true" />
        <cfargument name="username" type="string" required="true" />
            <cfmail to=arguments.to
           from="email@gmail.com"
           subject="Welcome to Bedrock"
           type="text">
            Dear User,

            We, here at Bedrock, would like to thank you for joining.

            Best wishes
            Barney
        </cfmail>
        <cfoutput>
           <p>Thank you, #arguments.username#, for registering.
            We have just sent you an email.</p>
        </cfoutput>
    </cffunction>   
</cfcomponent>

If this were named mycfc.cfc at the root of your application, you could call it from javascript using this url: /mycfc.cfc?method=sendEmail&to=test@example.com&username=test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM