PHP ::: 2 个 AJAX 调用 ::: 2 个响应 ::: 按顺序
这个问题已经被问过好几次了,但已经得到了很好的解决。
在我看来,一个糟糕的实现是:
使用像 mootools 或 jquery 这样的库
在请求之间使用某种延迟,例如 setTimeout("doNext()",3000);
具体来说,我想要实现的目标就是这个。
<div onClick="javascript:one();two();"></div>
one() 和two() 是AJAX
one() 必须出现在two() 之前
This question has been asked several times already, but has been solved with poor implimentations.
A poor implimentation in my opinion is something that:
Uses a library like mootools or jquery
Uses some sort of delay in between requests like setTimeout("doNext()",3000);
Specifically what I am trying to achieve is this.
<div onClick="javascript:one();two();"></div>
one() and two() are AJAX
one() MUST come before two()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
two();
必须在one()
中的 ajax 成功处理程序中调用。例如:html:
javascript:
two();
has to be called in the success handler of the ajax inone()
. E.g.:html:
javascript:
试试这个!
Try this !!!
我同意 scessor 的观点,即第二个函数应该在第一个函数内执行 - 如果成功的话。这是我的 AJAX 函数,没有任何 jquery 或其他需要的:
第二个函数是相同的,但没有成功时的函数调用:
ajax_call_1 函数参数是:
divId :将要调用的 html 元素的 id保存 AJAX 调用后的结果
params :应与请求一起发送的参数,即 id、名称或您需要发送的某种其他值。
说实话这两个函数的复用性不太好。我正在将它们更改为更加抽象,以便它们可以用于每 2 个同时进行的 AJAX 调用,但目前它们做得很好。
I agree with scessor that the second function should be executed within the first one - on success. Here is my AJAX function, without any jquery or else needed:
And the second function is the same, but without a function call on success:
ajax_call_1 function arguments are:
divId : id of the html element that will hold the result after the AJAX call
params : parameters that should be sent with the request i.e. id, name or some sort of other values you need to send.
To be honest this 2 functions are not very reusable. I am in progress of changing them to be more abstract so they can be used for every 2 simultaneously made AJAX calls, but for now they do a good job.