0

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?

techie_28
  • 2,025
  • 4
  • 37
  • 56
  • Are you using the same variable to hold the ajax response in both ajax call ? – Mayank Pandeyz Aug 16 '16 at 09:44
  • 2
    Way too much code, no syntax colouring, this is not pleasant to work with. Please edit your question, post your code here and format it properly, and also try to narrow it down. – Jeremy Thille Aug 16 '16 at 09:45
  • @JeremyThille actually I tried to elaborate the most because many global objects/vars are there – techie_28 Aug 16 '16 at 09:49
  • @MayankPandey no they are from different functions – techie_28 Aug 16 '16 at 09:52
  • @JeremyThille I updated the pastebin link in the question to have JS syntax highlighting.. hope that is ok – techie_28 Aug 16 '16 at 09:52
  • It's okay, but I can't find what's wrong. Each ajax call is made in its own scope, and the two `data` returned should be completely independent. As a workaround/debug attempt, try renaming them `data1` and `data2`, see what happens. Having different names, they can't override each other, so if the problem persists, that's not what's happening and the problem is elsewhere. – Jeremy Thille Aug 16 '16 at 10:10
  • @JeremyThille Yes I ve tried that..could this be a server side issue?The backend is in Java. – techie_28 Aug 16 '16 at 10:24
  • I ve tried disabling cache for this but it did not work as well – techie_28 Aug 16 '16 at 10:34
  • @JeremyThille perhaps this could be the cause? http://stackoverflow.com/questions/5583478/java-servlets-ajax-requests-come-back-with-mixed-responses – techie_28 Aug 16 '16 at 10:54
  • Apparently that's the same problem... I never stumbled upon that, but it's interesting to know – Jeremy Thille Aug 16 '16 at 11:57
  • @JeremyThille can you please post the link http://stackoverflow.com/questions/5583478/java-servlets-ajax-requests-come-back-with-mixed-responses as an answer to this question?I am unable to answer my own question & its getting posted as a comment each time. – techie_28 Aug 26 '16 at 10:54
  • Sure, did so. It's weird you can't answer your own question – Jeremy Thille Aug 26 '16 at 11:10

1 Answers1

1

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

Community
  • 1
  • 1
Jeremy Thille
  • 21,780
  • 7
  • 36
  • 54