1

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 protected]"
       from="[email protected]"
       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>
5
  • 2
    You will probably have better luck with your sendMail function in a .cfc file rather than a .cfm. Commented Dec 29, 2014 at 18:39
  • 1
    Change your returntype to "any" for the output to come back. As @DanBracuk mentioned, I've also always used a cf component for this. Commented Dec 29, 2014 at 19:26
  • Thanks for your help. Can you perhaps post an example of how I can include this code in a .cfc? I assume you mean to have a separate file and include it. Thanks. Commented Dec 29, 2014 at 19:48
  • 1
    You don't include a CFC. You create a connection to it using createObject(). Commented Dec 29, 2014 at 22:45
  • 1
    You can also invoke cfc's via url, as long as the component function is marked access=remote. ie /path/to/YourComponent.cfc?method=someMethodName. That is what your original example was trying to do. The reason it did not work is because that syntax is only supported with .cfc's - not .cfm scripts. Commented Dec 30, 2014 at 2:25

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The key is 1) use a CFC, 2) use "remote" as the access attribute for the function 3) use "?method=functionName" in the "get" string. it's not difficult, but you are missing a few steps.
Thanks. This answer looks good. I'm going to try this. I'll try to let you know what happens, as I won't get back to this project for a few days.
Hmm. i tried this and got a "cross-origin request" error from the browser. So I got the "before" and "after" alerts, but nothing in the middle. Any suggestions?
0

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.

3 Comments

Downvoting because you need not use cfajaxproxy for this.
@ScottStroz We need not use cfajaxproxy. I understand this. But this can be one of the way to deal with Cf function call via JS.I just wanted to make the guy (user3281588) aware of this option.
I understand why you mentioned it, I just do not believe in offering bad advice, and suggesting the use of cfajaxproxy would fall under that category. The ColdFusion JS/UI features are less than optimal, poorly implemented, outdated, and severely limited. It is better to learn how to do things like this the right way - with jQuery or other JavaScript library.
0

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 protected]"
           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&[email protected]&username=test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.