简体   繁体   English

连续调用jQuery.ajax会导致单个回调

[英]calling jQuery.ajax consecutively results in single callback

this is a interesting problem. 这是一个有趣的问题。

i am doing an asynchronous ajax put 我正在做异步ajax put

    return $.ajax({
        url: url,
        type: 'PUT',
        contentType: 'application/json; charset=utf-8',
        async: true, // default
        success: function (result, textStatus, xhr) {...}

this works as expected, unless a user does a put before previous call returns (even though it's async, the call does take .5 second to complete) 这可以按预期工作,除非用户在上一次调用返回之前执行put(即使它是异步的,调用确实需要.5秒才能完成)

if a user presses the button a few times (executing multiple puts) the following happens: 如果用户按下按钮几次(执行多次放置),则会发生以下情况:

  • i see only one server call in fiddler 我看到fiddler中只有一个服务器调用
  • success gets fired for every click 每次点击都会成功
  • all callbacks get the same new row ID (returned by the server) 所有回调都获得相同的新行ID(由服务器返回)

this leads me to inevitable conclusion that the first server callback triggers all outstanding callbacks.. 这导致我不可避免的结论是第一个服务器回调触发了所有未完成的回调。

i could disable the button until the callback returns, but is it possible to handle multiple outstanding calls? 我可以在回调返回之前禁用该按钮,但是是否可以处理多个未完成的呼叫? is this a browser limitation? 这是浏览器的限制吗? best way to handle this? 处理这个的最佳方法?

UPDATE UPDATE

as a test i switched to using POST instead of PUT: adjusted type: 'POST' on JS side, and [HttpPost] on web api (server side). 作为测试我切换到使用POST而不是PUT:调整类型:JS端的'POST'和web api(服务器端)的[HttpPost]。

the behavior did not change. 行为没有改变。

UPDATE UPDATE

looking at posts like this one .. this really should work. 看看像这样的帖子..这真的应该有效。 i don't see any specific reason why the rest of concurrent requests are not not making it out to the server. 我没有看到任何具体的原因,为什么其余的并发请求不会发送到服务器。

Shouldn't PUT requests be idempotent? PUT请求不应该是幂等的吗? That is, submitting multiple requests should generate the same response? 也就是说,提交多个请求应该生成相同的响应? If so, the code may simply be trying to coalesce your identical PUT requests since they should all end up with the same result. 如果是这样,代码可能只是试图合并你的相同PUT请求,因为它们都应该以相同的结果结束。 If you're incrementing some ID for every post (ie changing server state) then you should be using POST instead of PUT. 如果你为每个帖子增加一些ID(即改变服务器状态),那么你应该使用POST而不是PUT。

This may not fix your issue; 这可能无法解决您的问题; it's just a thought. 这只是一个想法。

You can't wait for an async callback in javascript. 你不能等待javascript中的异步回调。 You have to restructure your code to do all future work based on the async response from the actual callback. 您必须重新构建代码,以根据实际回调的异步响应执行所有未来的工作。

If you need to make multiple consecutive ajax calls, then you issue the first one and in the success handler or response handler for the first ajax call, you issue the second ajax call and in the response handler for the second one, you carry out whatever you want to do with the data 如果你需要进行多个连续的ajax调用,那么你发出第一个ajax调用,在第一个ajax调用的成功处理程序或响应处理程序中,你发出第二个ajax调用,在第二个调用处理器中,你执行任何调用你想要做的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM