Solved

Coldfusion Ajax function question

Posted on 2014-01-25
8
376 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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&param1=aaaa&param2=bbbbb
0
 

Author Closing Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

22 Experts available now in Live!

Get 1:1 Help Now