JSONP Long Polling always loading

你说的曾经没有我的故事 提交于 2019-12-03 17:25:59

This is a simple fix.. All you have to do is start your polling request with a setTimeout..

Here is some code I use.. It uses jQuery, but I assume you can figure out what you need to and use your library to do the same.

<script type="text/javascript">
  function poll(){
    $.getJSON('/updates', function(json){
      //reconnect since we successfully received data and disconnected
      poll();

      //add something here to do whatever with the recieved data
    });
  }
  /*call the poll function after document has loaded with setTimeout
  if called before the document finishes loading completely it will
  cause a constant loading indication*/
  setTimeout(poll, 1);
</script>
ssokolow

I don't have an answer, but I do have a suggested alternative. Someone else just asked a similar question and here's my answer.

Basically, if you have control of the server, the simplest solution is to use Cross-Origin Resource Sharing headers to OK cross-domain XMLHttpRequest and fall back to JSONP on old browsers.

I've provided a reasonably complete compatibility table (every userscript-capable browser) for CORS as part of the answer I linked to, as well as a more general one on Wikipedia.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!