Solved

Coldfusion Ajax function question

Posted on 2014-01-25
8
378 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
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
 

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
3 Use Cases for Connected Systems

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

 
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 15

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX Slow 15 57
json example 39 134
Adding checkmark icon to product added in cart using PHP/Jquery 4 75
Coldfusion UNIX/EPOCH Time Conversion - Time Zone? 11 23
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now