techie_28
techie_28

Reputation: 2133

jQuery Ajax: Sucess Response parameter overwriting

I am having a strange problem where the response param of 1st ajax call is being overwritten by the 2nd call's param. Code is:

http://pastebin.com/degWRs3V

When both drawDonutForExternalLogin & drawDonutForExtUser are called consecutively,the response variable of the later request(data param of success handler) overwrites the data param of 1st request.

The calls do complete almost at the same time always but when there is a difference this problem doesn't occur.The data set is also fine when the 2nd function is invoked from the success handler of 1st function.

Why the data param becomes identical when the calls are consecutive and finish at the same time?

I tried debugging the server side code by placing breakpoints but that also provides the delay between ajax requests thereby yielding correct result.

Any ideas please?

Upvotes: 0

Views: 280

Answers (1)

Jeremy Thille
Jeremy Thille

Reputation: 26360

Copied from stackoverflow.com/questions/5583478/java-servlets-ajax-requests-come-back-with-mixed-responses to OP's request :

The likely cause is that the servlets are not written to be thread safe. Note that the object that contains the servlet methods may be used to respond to many simultaneous requests. If that method uses a class level variable to create the response, then requests will appear to get 'mixed up'.

So.. Request #1 comes in, is assigned to an instance of Servlet, Instance #1

The appropriate method is invoked on Instance #1, which starts using a class variable to calculate the result. Instance #1.myVariable = "Blah"

Now, Request #2 comes in, is also assigned to Instance #1

Again, the appropriate method is invoked on Instance #1, which sets Instance #1.myVariable ="Foo"

.. in the mean time the first request completes, and returns Instance 1.myVariable... "Foo"!

.. and then the second request completes, and also returns "Foo".

Upvotes: 1

Related Questions