Question

I have a ColdFusion function that checks if a record exists in a table, and if it does it returns data for the row found. However, if the record does not exist, I would like to have it make a call to a JavaScript function that I've written and use the value that is returned from the JS function to continue processing in the CF function. I know that CF is written in Java, so I'm hopeful that there is someway to perform this task.

Basically, from CF page, the steps would be...

  1. Call CF Function located in my functions.cfc file
  2. Within the CF function, make call to JavaScript function and wait for response
  3. Continue processing in CF function using the value returned from the JS function.

Any ideas? Thank you for any help!

Was it helpful?

Solution 2

Was over complicating things trying to use javascript for the ajax call. Just switched over to using cfhttp and all is working as needed. DeserializeJSON method of CF works great to parse the json object returned from the Google API.

OTHER TIPS

You need to understand that CF and JS do not exist in the same environment, and cannot interact with each other like that: http://blog.adamcameron.me/2012/10/the-coldfusion-requestresponse-process.html.

Your JS can make a remote call to CF, but CF cannot initiate a call to JS based on its processing, because whilst CF is processing, everything is still on the server, so JS is not part of the recipe.

The best you can do is to have the CF code write out JS code which is then sent to the browser, which is then executed when the browser comes to execute JS during page rendering.

But that really doesn't fit with what you are wanting to do here.

Anything you do has to fit into the request/response life cycle.

You can do this:

  1. from an already loaded page, make a JS call to a method in functions.cfc
  2. functions.cfc's method call can only process and then return something to the JS that fired the request in 1.
  3. JS can then make a decision as to what to do next, perhaps firing another request back to functions.cfc to continue processing.

What you can't do is have the step at 2 both call back to JS and continue processing and then return something else to JS later on. Each request can only have one response. So you might have to use multiple requests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top