简单的Ajax调用似乎被阻止 [英] Simple ajax call seems to be blocking

查看:66
本文介绍了简单的Ajax调用似乎被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的很简单的问题。我想测试我正在开发一个RESTful Web服务,并有这个简单的ajax调用(使用jQuery):

Really simple question. I trying to test a Restful webservice that I am developing, and have this simple ajax call (using 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>

这个运行时加载页面。由于它的运行,页面被拦截;即,(我可以看到旁边的鼠标指针沙漏)没有其他的用户操作可以处理。 (顺便说一句,这个特殊的GET请求 - 故意 - 需要很长的时间才能恢复)

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

这是为什么? A(异步)JAX吧?很显然,我想提出一个初学者的错误。任何想法,请?

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

当我试图用这个普通的JavaScript(没有图书馆),它将按预期工作。这是否有东西做与XHR的onreadystatechange的jQuery的处理?

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?

感谢您寻找。

编辑:多人建议设置异步:true,这正好是默认的jQuery的,因此没有任何影响

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

编辑:previously提到的,如果我使用纯javascript和一个计时器启动这一点,例如, window.setInterval(函数(){startLongPoll();},5000) 它更新如预期,没有出现阻塞。思想,任何人吗?

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?

推荐答案

下面是我做过什么来解决这个问题的一个例子:

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.
});

注:我使用的是速记jQuery的方法的getJSON,这是一个包装与数据类型设置为json的Ajax调用。然而,这种解决方案将适用于所有Ajax请求。

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.

引用

<一个href="http://stackoverflow.com/questions/1064782/stop-the-browser-throbber-of-doom-while-loading-comet-server-push-iframe/1066729#1066729">Stop浏览器&QUOT;活动指示器厄运&QUOT的;在加载彗星/服务器推送iframe中

这篇关于简单的Ajax调用似乎被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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