Ceazar Festin
Ceazar Festin

Reputation: 5

Ajax and session variable

Imagine this scenario:

  1. I have a link that fires up a JavaScript function(has an Ajax request in it).

  2. The JavaScript function has a parameter passed by the link's onclick='jsFunction(param)'.

  3. The JavaScript function sends the parameter value in a function inside a controller that sets the param value to a session variable.

  4. The function in the controller will then send the new value of the session variable back to the JavaScript function's Ajax request.

  5. When the data reached into the Ajax request, another function will be called using the data being passed.

Question:

Since I am talking about the session variable's value being passed to an Ajax request, how can I process the data came from the link to the second function in real-time? I want to post the code here but it's too verbose with the info I only need.

The current state of my code is that, I can't fetch the right/latest data. Instead, I get the previous data came from the previous link I clicked.

Is there an Ajax feature that I can only proceed to the next function if the first function is done processing the latest data?

Any help will be much appreciated.

Upvotes: 0

Views: 1486

Answers (1)

user1680049
user1680049

Reputation:

You can use the callback to any of the jQuery AJAX methods to delay execution of another function until after the request is complete.

   $.post('/some/url', somedata, function(returnData) {
        // put the code you want to execute on completion here
   });

or If you have already defined your function to be called after returning data, you can just write like this:

   $.post('/some/url', somedata, processData);

Upvotes: 2

Related Questions