Solved

Coldfusion Ajax function question

Posted on 2014-01-25
8
383 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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