Solved

Coldfusion Ajax function question

Posted on 2014-01-25
8
382 Views
Last Modified: 2014-02-09
I'm using <cffunctions in regular .cfm pages (not .cfc). I've followed this code pattern from a couple of other developers and I know it's probably not standard practice but I find it convenient and easier to work with sometimes. So, in a ajax call is it still possible to call functions or methods with a .cfm extension file? I haven't been able to do so yet. My code below is what I've been working with.

$.ajax({
        type: "POST",
        url: "myfunctions.cfm?method=myfunctionname",
        data: "name=Tom",
        cache: false,
        success: function() {
          alert("yes");
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }

    })

Open in new window


<cffunction name="deleteName"
			access="public"
			output="false"
			returntype="void"
			hint="delete name from users"
			>

<cfargument name="name"
				required="true"
				default=""
				type="string"
				hint="name">

	<cfquery datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
	DELETE
	FROM users
	WHERE name = '#arguments.name#'
	</cfquery>

</cffunction>

Open in new window

0
Comment
Question by:brihol44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 84 total points
ID: 39810073
I find it convenient and easier to work with sometimes

Look like not this time.
Why don't you want to use a CFC ?
0
 

Author Comment

by:brihol44
ID: 39810568
lol... ok. I guess deserved that. So "No" then?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39810597
You can always open a page and if there's some code behind reading passed parameters run a function so the answer is yes, that just mean you're not calling expressively the function
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:brihol44
ID: 39810605
ok, I tried that but I do have several functions on that page. It's not just the one cffunction I included in my example. I guess I didn't know how to run my example with it knowing what function to actually use.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39810612
All the function inside the page need to worry about if the << method >> parameter function is specified in the URL (query string) then DON'T run.
Doable but so bad, no?
0
 
LVL 16

Assisted Solution

by:Gurpreet Singh Randhawa
Gurpreet Singh Randhawa earned 83 total points
ID: 39811235
There is no harm in doing that, Indeed I say that for cffunctions and other things CFC is a better approach as it provides many functionalities like a component inheritance and others, but if you do this way, it will not effect the performance or anything, it will be just like another way of doing the code, it does not called as a Best Practice but it is workable and sometimes you can even use other things in that like suppose you have a cfm page

1. You have written couple of functions in that Page
2. You have some custom Tag Call in that Page
3. You have written Some Jquery Script wrapped with cfsavecontent tag and served in header through cfhtmlhead.
4. Many other functionalities like that CFM page itself has functionality which is showing on the web Page when you are browsing, irrespective that cffunction is being called or not called.

I have seen many developers doing it, so accordingly from my side no Harm, But just a not a Good Practice

I have even seen developers using the Text Files being served as ColdFusion includes writing coldfusion code in the text files, well that is not acceptable but people still use it

Regards
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 83 total points
ID: 39812295
So, in a ajax call is it still possible to call functions or methods with a .cfm extension file?

Like with a .cfc? No, not automatically.  As others mentioned, you'd have to code it yourself using some sort of switch/case or cfif to decide which function to run  based on the URL parameters supplied. For example, using the jquery above:

         url: "myfunctions.cfm?method=myfunctionname"

... in the .cfm page do something like this:

      <cfparam name="url.method" default="">

      <cfswitch expression="#url.method#">
            <cfcase value="deleteName">
                  <!--- invoke deleteName function --->
                 <cfset deleteName(argumentCollection=URL)>
            </cfcase>
            ... handle other methods
       </cfswitch>

While that works, it's duplicating what cfc's do already. IMO you're better off with a .cfc. Keep in mind you can always test cfc's manually by setting access=remote, and running the URL in your browser:

         http://localhost/path/to/yourComponent.cfc?method=yourMethodname¶m1=aaaa¶m2=bbbbb
0
 

Author Closing Comment

by:brihol44
ID: 39846441
All good stuff and the feedback I was looking for. Thank you!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question