简体   繁体   中英

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 . For example, how can I serve content from example.com and make an AJAX call to example.org every 30 seconds.

The canonical solution to this question is a JSONP call in a <script> tag. However, the <script> tag is just loaded once. 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?

Consider the following code. We're using setInterval to call our ping() function every 3 seconds.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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