jQuery Ajax:成功响应参数覆盖 [英] jQuery Ajax: Sucess Response parameter overwriting

查看:176
本文介绍了jQuery Ajax:成功响应参数覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,即第一个ajax调用的响应参数被第二个调用的参数覆盖. 代码是:

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

drawDonutForExternalLogin 和依次调用 drawDonutForExtUser ,后一个请求的响应变量(成功处理程序的数据参数)将覆盖第一个请求的数据参数.

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?

我尝试通过放置断点来调试服务器端代码,但这也提供了ajax请求之间的延迟,从而产生正确的结果.

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

请问有什么想法吗?

推荐答案

可能的原因是,这些servlet没有写为线程 安全的.请注意,包含servlet方法的对象可能是 用于响应许多同时请求.如果该方法使用 类级别的变量来创建响应,然后将出现请求 变得混乱".

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

所以..请求#1进入,被分配给Servlet的一个实例, 实例#1

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

在实例1上调用适当的方法,实例1使用 类变量来计算结果.实例#1.myVariable = 布拉"

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

现在,请求#2进入了,并且也分配给了实例#1

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

同样,在实例1上调用适当的方法,该实例将设置 实例#1.myVariable ="Foo"

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

..在此期间,第一个请求完成,并返回Instance 1.myVariable ..."Foo"!

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

..然后第二个请求完成,并且还返回"Foo".

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

这篇关于jQuery Ajax:成功响应参数覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆