简体   繁体   English

嵌套另一个Ajax请求时,Ajax响应处理被中断

[英]Ajax response processing interrupted when nesting another Ajax request

I'm using Ajax requests via Jquery to update some parts of a web page. 我通过Jquery使用Ajax请求来更新网页的某些部分。

Depending on the data values retrieved from the first Ajax request I send (or not) another nested Ajax request to get some more information. 根据从第一个Ajax请求中获取的数据值,我发送(或不发送)另一个嵌套的Ajax请求以获取更多信息。 Meaning the second Ajax request is nested in the first Ajax response handler. 这意味着第二个Ajax请求嵌套在第一个Ajax响应处理程序中。

Basically: 基本上:

// Get some information
$.ajax({type: "POST", url: get_some_info_url})
  .done(
    function(msg)
    {
      // Given the info contained in msg, let send another request to get more
      if(msg.some_info === true)
      { 
        $.ajax({type: "POST", url: get_some_more_info_url})
           .done(
                function(msg2)
                {
                    // Do something
                }); 
      }

      // Display "Hello world!" whatever the msg.some_info value is.
      $('div#message-id').html("Hello world!");

    });

Different scenarios: 不同的情况:

  1. If msg.some_info is TRUE => no displaying of "Hello world!" 如果msg.some_info为TRUE => 不显示 “ Hello world!”

  2. If msg.some_info is FALSE => displaying of "Hello world!" 如果msg.some_info为FALSE => 显示 “ Hello world!”

  3. More strange: If I set a breakpoint in the Firefox debugger on the line containing the second nested Ajax call (just stepping over the $.ajax call in the debugger) and msg.some_info is TRUE => displaying of "Hello world!" 更奇怪的是:如果我在包含第二个嵌套Ajax调用的行中的Firefox调试器中设置了一个断点(只是跳过调试器中的$ .ajax调用),而msg.some_info为TRUE => 显示 “ Hello world!”

I'm a little confused here. 我在这里有点困惑。 Why does my second Ajax call prevent the rest of the code of the first Ajax response from being executed? 为什么我的第二个Ajax调用会阻止执行第一个Ajax响应的其余代码? Is there some "race condition" or session management stuff? 是否存在某些“竞赛条件”或会话管理内容?

And why breaking in the code "resolves" the issue? 以及为什么破解代码可以“解决”问题?

EDIT : ISSUE RESOLVED. 编辑 :问题已解决。 See my answer. 看我的答案。

Thanks for your help. 谢谢你的帮助。

I'm not sure you should test for truth with === operator. 我不确定您是否应该使用===运算符测试真相。

According to your comments, 根据您的评论,

// Given the info contained in msg, let send another request to get more

you just want to check if 你只想检查一下

msg.some_info

exists at all. 存在。 In this case it's better to check by writing 在这种情况下,最好通过写检查

if(msg.some_info) {
    /* Your code here*/
}

Another variant why msg.some_info is true but the "Hello world" isn't displayed is that your code hasn't response from the server and waits for it. msg.some_info为true但未显示“ Hello world”的另一个变体是您的代码未从服务器响应并等待它。 Because you wrote "just stepping over the $.ajax call in the debugger) and msg.some_info is TRUE => displaying of "Hello world!"". 因为您写了“只是跳过调试器中的$ .ajax调用),而msg.some_info为TRUE =>显示“ Hello world!”。 So you step over that "bad point" and the message is diplayed than. 因此,您越过了这个“坏点”,消息就被打乱了。 That makes me think that there're some problems either in the code itself or in the server response which is not got back. 这使我认为代码本身或服务器响应中都存在一些问题,这些问题没有解决。

The issue was on my side, sorry for wasting your time. 问题在我这边,很抱歉浪费您的时间。

Actually I was erasing the "hello world!" 实际上,我正在删除“ hello world!” text in the second Ajax response processing. 文本在第二个Ajax响应处理中。 Hence the difference between debugging run and normal run. 因此,调试运行和正常运行之间的区别。 The sequence for handling ajax responses was not the same with and without debugging hence misleading me about the root cause of the issue. 有和没有调试,处理ajax响应的顺序是不同的,因此使我误解了问题的根本原因。

Thank you all for your help. 谢谢大家的帮助。

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

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