How to get a response from servlet via ajax?

What are good ways to ensure AJAX requests called in a certain order get the response in the same order?

  • Answer:

    Cache the responses in a numerically indexed object that corresponds to a a handler index. Determine some form of "readyness" and poll the object for changes to its state. Once the object has reached that "readyness" state, execute the responses in the order that they were indexed.

Rick Waldron at Quora Visit the source

Was this solution helpful to you?

Other answers

While coding a grid based UI for TV listings (at http://tv.burrp.com/scheduler/view.html) I wished the same :). Unfortunately, in a multi-threaded backend there is no guarantee whatsoever on which incoming HTTP request gets executed first. The last request in queue might come back first with a response. There are two possible ways here - Write a simple JS implementation of Queue to stack all the AJAX requests. Fire only when you have received a response for the penultimate one. This also means that you have to use a an AJAX (say XMLHttpRequest) wrapper. This is not what you asked for but might be something worth pondering over - with each AJAX request, you can generate a unique ID on the frontend, send to the server with the request and have it send it back to you in the response. In your frontend you can keep a map of such ID's versus whatever stateful object you want to store against so that you can map the incoming responses (irrespective or order) to these objects.

Avlesh Singh

The jQuery plugin mentioned by looks good. You could also take a look at the IO Queue in YUI http://developer.yahoo.com/yui/3/io/#queue

Juan Ignacio Dopazo

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.