Coldfusion Ajax function question

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.

        type: "POST",
        url: "myfunctions.cfm?method=myfunctionname",
        data: "name=Tom",
        cache: false,
        success: function() {
        error: function (xhr, ajaxOptions, thrownError) {


Open in new window

<cffunction name="deleteName"
			hint="delete name from users"

<cfargument name="name"

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


Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

_agx_Connect With a Mentor Commented:
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)>
            ... handle other methods

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:

leakim971Connect With a Mentor PluritechnicianCommented:
I find it convenient and easier to work with sometimes

Look like not this time.
Why don't you want to use a CFC ?
brihol44Author Commented:
lol... ok. I guess deserved that. So "No" then?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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
brihol44Author Commented:
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.
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?
Gurpreet Singh RandhawaConnect With a Mentor Web DeveloperCommented:
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

brihol44Author Commented:
All good stuff and the feedback I was looking for. Thank you!
All Courses

From novice to tech pro — start learning today.