简体   繁体   English

如何定期对跨域服务器进行 AJAX 调用

[英]How to make periodic AJAX calls to cross-domain server

I am trying to figure out how to make periodic AJAX calls to a cross-domain hostname .我想弄清楚如何对跨域主机名进行定期 AJAX 调用。 For example, how can I serve content from example.com and make an AJAX call to example.org every 30 seconds.例如,我如何提供来自example.com内容并每 30 秒对example.org进行一次 AJAX 调用。

The canonical solution to this question is a JSONP call in a <script> tag.此问题的规范解决方案是<script>标记中的JSONP 调用 However, the <script> tag is just loaded once.但是, <script>标签只加载一次。 Therefore, it can not generate periodic calls to another server - just a single call when the page loads.因此,它无法生成对另一台服务器的定期调用——在页面加载时只是一次调用。

Is there a way to make a periodic AJAX call to a cross-domain server?有没有办法对跨域服务器进行定期AJAX 调用?

Consider the following code.考虑以下代码。 We're using setInterval to call our ping() function every 3 seconds.我们使用setInterval每 3 秒调用一次我们的ping()函数。

<script type="text/javascript">
  function ping() {
    $.ajax({
      ...
    });
  }

  setInterval(function() {
    ping();
  }, 3000);
</script>

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

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