1

I am new to jQuery API documentation and hence wondering if the following scenario is possible:

So far, I have come across only one Ajax call in one coldfusion page.

Scenario:

I am sending a $.ajax call to a cfc to call a method in a CFC and returning the data back in JSON format. Basically the method I am calling here will be adding the data into the database.

I want to write another $.ajax call to call a method in a CFC, which will read the data which is saved by the above ajax call and display in JSON format on the Web Browser.

So, my questions is, can I go for multiple ajax call in a single coldfusion page as long as Iam doing this in sequential manner?

Sequential order must be the key, am I right?

Please clarify !!

Thanks

7
  • There is no reason why this would not work ;) you can have as many ajax calls you want on the page. The question is that why would you want to write the entry into the database, and then retrieve the same data again with a second call? You can always pass the data submitted by the first ajax into a variable, and use that instead. Commented Nov 5, 2013 at 17:23
  • Thanks. So, sequential order is the key in my scenario, right? Commented Nov 5, 2013 at 17:30
  • 1
    Logically, yes, but it also depends what kind of things are you intending to do with the second ajax call. If sequence is important, you will need to handle the error thrown if the db entry is not found during the second call (e.g. if the first call failed, or that you have problems writing to the db) Commented Nov 5, 2013 at 17:32
  • Don't forget to set your ajax calls to async=false btw Commented Nov 5, 2013 at 17:59
  • async=false is not needed here. Commented Nov 5, 2013 at 18:36

1 Answer 1

1

There's two ways to go about it really...

A jQuery AJAX call has a callback function to it, so you could simply get some JSON back from the initial submission and from that callback fire off the second request - that way you can be sure the first action has completed before you call the second.

var request = { returnformat : 'json', queryformat : 'column', method: 'save_data', data: /* data to pass to the save action here */ } $.getJSON("path/to/your/service.cfc", request, function(response) { var request = { returnformat : 'json', queryformat : 'column', method: 'get_data' } $.getJSON("path/to/your/service.cfc", request, function(response) { /* code to fire on response */ }); }); 

However I think a better way would be to make a single AJAX call and simply do both the submission save and the data response in one CFC method. Your CFC might look something like this:

<cffunction name="save_response" access="remote" output="false"> <cfset response = structNew()> <cfset response.status = true> <cftry> <cfquery datasource="#datasource#"> INSERT... </cfquery> <cfquery datasource="#datasource#" name="get_data"> SELECT... </cfquery> <cfset response.data = get_data> <cfcatch type="any"> <cfset response.status = false> </cfcatch> </cftry> <cfreturn response> </cffunction> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.