javascript 简单的 ajax 调用似乎被阻塞

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5437981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:12:06  来源:igfitidea点击:

Simple ajax call seems to be blocking

javascriptjqueryajaxxmlhttprequest

提问by meta.matt

Really simple question. I trying to test a Restful webservice that I am developing, and have this simple ajax call (using jquery):

真的很简单的问题。我试图测试我正在开发的 Restful webservice,并进行这个简单的 ajax 调用(使用 jquery):

 <script type="text/javascript">  
   $(document).ready(function() { 
     var url = '/index.php/gettest/reallyLongRequest';    
     $.ajax({
       url: url,
       dataType:'text',
       success:function(data) { $('#result').html(data);},
       error:function(xhr,err,e) { alert ("Error: " + err);}
     });                
  });
 </script>

This runs when the page loads. As it's running, the page is blocking; i.e., (I can see the hourglass next to the mouse pointer) no other user actions can be handled. (Btw, this particular get request--intentionally--takes a very long time to return).

这会在页面加载时运行。当它运行时,页面被阻塞;即,(我可以看到鼠标指针旁边的沙漏)无法处理其他用户操作。(顺便说一句,这个特殊的获取请求——有意——需要很长时间才能返回)。

Why is this? A(asynchronous)JAX right? Obviously I am making a beginners mistake. Any ideas, please?

为什么是这样?A(异步)JAX 对吗?显然我犯了一个初学者的错误。有什么想法吗?

When I attempt this using plain javascript (no library) it works as expected. Does this have something to do with Jquery's handling of the xhr onreadystatechange?

当我尝试使用普通的 javascript(没有库)时,它按预期工作。这与 Jquery 对 xhr onreadystatechange 的处理有关吗?

Thank you for looking.

感谢您的关注。

EDIT: multiple people have suggested setting async: true, which as it happens, is the default in jquery, and as such has no effect.

编辑:很多人建议设置 async: true,它恰好是 jquery 中的默认值,因此没有任何效果。

EDIT: As previously mentioned, if I use plain javascript and start this with a timer, e.g., window.setInterval(function() { startLongPoll(); }, 5000)It updates as expected, without appearing to block. Ideas, anyone?

编辑:如前所述,如果我使用普通的 javascript 并使用计时器启动window.setInterval(function() { startLongPoll(); }, 5000)它,例如,它会按预期更新,而不会出现阻塞。想法,有人吗?

采纳答案by meta.matt

Here is an example of what I did to solve the problem:

这是我为解决问题所做的一个示例:

jQuery(document).ready(function() {
  setTimeout(function () {
   $.getJSON("veryLongRequest", function(json) {
   alert("JSON Result: " + json[0].id);});
  }, 500); // You may need to adjust this to a longer delay.
});

Note: I am using the short-hand jquery method, "getJSON" which is a wrapper for the ajax call with datatype set to "json". However, this solution will work for all ajax requests.

注意:我使用的是简写 jquery 方法“getJSON”,它是数据类型设置为“json”的 ajax 调用的包装器。但是,此解决方案适用于所有 ajax 请求。

Referenced:

参考:

Stop the browser "throbber of doom" while loading comet/server push iframe

在加载彗星/服务器推送 iframe 时停止浏览器“末日的悸动”

回答by RSG

I think that this should default to true, but try adding async: trueto your ajax json parameter.

我认为这应该默认为 true,但尝试添加async: true到您的 ajax json 参数中。

回答by Francesco Terenzani

Does the code below work as expected?

下面的代码是否按预期工作?

 <script type="text/javascript">  
   //$(document).ready(function() { 
     var url = '/index.php/gettest/reallyLongRequest';    
     $.ajax({
       url: url,
       dataType:'text',
       success:function(data) { $('#result').html(data);},
       error:function(xhr,err,e) { alert ("Error: " + err);}
     });                
  //});
 </script>

回答by Chris Kooken

May want to try and Add async:true

可能想尝试并添加 async:true

<script type="text/javascript">  
       $(document).ready(function() { 
         var url = '/index.php/gettest/reallyLongRequest';    
         $.ajax({
           url: url,
           async:true,
           dataType:'text',
           success:function(data) { $('#result').html(data);},
           error:function(xhr,err,e) { alert ("Error: " + err);}
         });                
      });
     </script>