call a function in a cached coldfusion component object with ajax

Can I call a function in a cached coldfusion component object with ajax?

And I create the object like so:
<cfset x = new somefile().init() />
So I then would like an ajax url to query x specifying method and parameters

LVL 25
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
An AJAX call is no different from a URL in a browser or a post from a form. On the server side you would deal with exactly as you would a GET or POST from a page.

Set your function up to retrieve the GET / POST parameters as you normally would. Process the request according to your requirements and then output what your AJAX routine is expecting to receive.

To test - set your function up to GET parameters. Type the URL into your browser to your backend function with parameters - check the output - if it is what you are expecting - switch back to POST (if that is what you will be using with the AJAX Call) and wire the AJAX in the browser to the URL you just tested - filling in approprate POST variables if necessary.
dgrafxAuthor Commented:
Thanks for the response
The issue with cached objects (such as in the Application scope) is that only ColdFusion can read variables stored within that were created during init()
You can access the component or anything else with ajax yes - but trying to access the object doesn't work. You end up having to resend in your init vars at every call or something else undesirable.
The reason to create the object in the first place (one of the reasons) is to have your data and functions and variables in a neat little package that you can query without needing to send in url params every time you want to call a function.
So what I'm experimenting with is a proxy of sorts which would be a ColdFusion cfc file that is accessed directly by ajax and it can then read into the ColdFusion object that ajax can't. And of course the object can return data to the proxy which in turn can return it back to the ajax call.
I asked this question hoping that someone knew of this issue and had some insight.

I will post back later and thanks again !
Julian HansenCommented:
You end up having to resend in your init vars at every call or something else undesirable.
Let me ask you this.

How do you solve this problem when you have a page with a link that goes to another page?

How would you get access to your init() vars in the second page?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

dgrafxAuthor Commented:
To clarify - this is only a problem interacting with objects - not navigating from page to page or any normal web activities.
When the application starts (you can do this manually as well) your objects are initialized and you've sent in variables for each object to cache inside itself - typically using an init(var1=someval,var2=someotherval,etc). These vars are usually values that aren't going to change such as datasource is a common one to send in - but whatever they are your object knows what they are and you don't need to send them in again.
BUT ajax (or a url for that matter) cannot access an object because it's not a file - it's in memory.
You can access the file that contains the function that is exactly the same as the object but it's a different instance and it doesn't have knowledge of these vars I speak of. It's like it's a copy - not the real thing and you need to send in your vars.
So with some research I discovered that you can use a proxy cfc that your ajax can communicate with because it's a file but it can interact with objects - an intermediary if you will - because of the fact that ColdFusion code can interact with ColdFusion objects.
dgrafxAuthor Commented:
So I found the solution which is to use a proxy file to act as an intermediary between the AJAX call and the object in memory

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Web pages are stateless - to maintain state across pages requires storing state
a) In a session
b) A cookie
c) local storage
d) In the page itself

Each request to the server is a "clean slate" request - the server has no "memory" of your previous interaction with the server.

Usually state is managed with or with the help of sessions. Data is stored in the session or an id is stored in the session / cookie that is then linked to some persistent storage like DB or file.

When you access a page on a server that remembers state, you access it by means of a URL which in turn loads a script which loads state and performs the task required of it.

A web request by means of a link, post from a form or AJAX call are identical in how they interact with the server - a request is made to the server by means of a URL and data is posted or included in the URL for the receiving script to process.

Hence the issue of using AJAX (the point I was trying to make) is identical to a form post or link click - you interact with the back end in the same way. Therefore, my point was, that if you construct your AJAX service to respond in the same way you would for any request to the server that is dependent on state - your AJAX call will work.

To make this work with ColdFusion requires putting in place on the CF side whatever you would put if this was a form submission. In other words - if you know how to solve this problem with a form post rather than an AJAX submission - then you have solved the CF part because it is the same process.
dgrafxAuthor Commented:
Thanks Julian
dgrafxAuthor Commented:
So I found the solution which is to use a proxy file to act as an intermediary between the AJAX call and the object in memory
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.