admin管理员组

文章数量:1332395

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!

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!

Share Improve this question edited Dec 21, 2013 at 18:20 James A Mohler 11.1k15 gold badges50 silver badges76 bronze badges asked Oct 11, 2013 at 15:17 PhilPhil 4,0699 gold badges71 silver badges117 bronze badges 4
  • Just insert it with script tags? – Sterling Archer Commented Oct 11, 2013 at 15:19
  • @RUJordan, not sure what you mean. Just insert it with script tags? The javascript function that I need to call performs an ajax call to the Google APIs to lookup info on a zip code. – Phil Commented Oct 11, 2013 at 15:23
  • All of your cf code and calls are done before the page is rendered and sent to the browser... so just put a CFIF statement around some javascript code so that it executes if receive the values back that you want. – steve Commented Oct 11, 2013 at 15:46
  • 1 Was over plicating 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. – Phil Commented Oct 11, 2013 at 15:55
Add a ment  | 

2 Answers 2

Reset to default 8

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 es 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.

Was over plicating 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.

本文标签: ajaxCall Javascript function from ColdFusion CFCStack Overflow