使用 JavaScript Ajax 轮询小部件

发布于 2024-10-03 16:12:20 字数 1407 浏览 0 评论 0原文

我有以下问题。我需要创建一个 JS 小部件并将其设置在一个博客上,例如来自 blogger.com 的任何博客。您可以选择一个 javascript 框,我将在该框中发布 JS。

我遇到的问题但不知道如何做到这一点是,脚本应该对 exmaple 进行 ajax 轮询 60 秒。但是,当主机与主机的链接不同时,如何执行ajax调用,其中包含JS?

例如,最简单的解释方法是:有一个搜索框,当你搜索任何内容时,JS 脚本应该从服务器流式传输结果 60 秒,这是我在脚本中设置的,与主机不同,其中JS 被包含在内,不会成为主机 JS 限制的问题。

或者例如聊天客户端,其中客户端托管在另一台主机上,服务器托管在另一台主机上。

谁能告诉我一个想法,或者给我一个如何做到这一点的例子?

谢谢 Nik


那么这个例子是否可能,但没有 JSONP?

function asyncreq(url) {
 var xmlhttp = false;
   try {
     xmlhttp = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         xmlhttp = false;
       }  
     }
   }
 if (xmlhttp){
         try {
             xmlhttp.open("GET", url);
             xmlhttp.onreadystatechange=function() {
                document.getElementById('mydiv').innerHTML = xmlhttp.responseText;
             }

             xmlhttp.send(null);
         }
         catch (failed) {
     xmlhttp = false;
   }
 }
}

如果您分块发送响应,那么一切都很好。但这又是ajax调用。当我在不同的主机中使用它时,由于同源策略,我无法调用该url。

还有别的办法吗?

我在此处发现了一个非常有趣的示例。

看一下底部,有一个求职框。如果您稍微调查一下,您会发现使用了类 RSL() 来执行请求。这个类如何在没有ajax的情况下执行请求?我无法理解哇这门课有效。谁能告诉我一个更好的例子吗?

I have the follow problem. I need to create a JS widget and set it on one blog, for example any blog from blogger.com. YOu can select there a box for javascript and I will post the JS in this box.

The problem what I have and don't know how to do this is, that the script should do an ajax polling for exmaple for 60 seconds. But how to execute an ajax call, when the host is not the same linke the host, where the JS is includet?

For example the easiest way to explai is: There is a search box and when enayone searches for anythign, then the JS script should streaming the results for 60 seconds from the server what I have set in the script and is different as the host, where the JS is includet, without to become a problem with the JS restriction for hosts.

Or for example a chat client, where the client is hosted on one other host and the server on another.

Can anyone tell me an idea, or send me an example how to do this?

Thanks
Nik


Well with this example is it possible but without JSONP?

function asyncreq(url) {
 var xmlhttp = false;
   try {
     xmlhttp = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         xmlhttp = false;
       }  
     }
   }
 if (xmlhttp){
         try {
             xmlhttp.open("GET", url);
             xmlhttp.onreadystatechange=function() {
                document.getElementById('mydiv').innerHTML = xmlhttp.responseText;
             }

             xmlhttp.send(null);
         }
         catch (failed) {
     xmlhttp = false;
   }
 }
}

If you send the response in chunks, then everything is fine. But here is the call in ajax again. And when I use it in a different host, then I can't call the url because of the same-origin policy.

Is there another way?

I found a very interesting example here.

Take a look at the bottom, there is a job search box. If you investigate a litte bit, then you will see there is a usage of a class RSL() which is doing the request. How this class is doing the request without ajax? I can't understand wow this class works. Can anyone show me a better example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-10-10 16:12:20

有两个主要选项:

  1. 将 iframe 放在您想要小部件所在的位置。它的 src URL 将位于接收 AJAX 调用的同一服务器上。

  2. 使用 JSONP,它包括在页面中插入脚本标记以绕过同源策略。这要求 AJAX 服务器将其 JSON 输出包装在 ?(...) 中,其中 URL 包含 callback=?。然后,一旦收到响应,就启动另一个请求。

There are two main options:

  1. Put an iframe where you want the widget to go. Its src URL would be on the same server that will receive the AJAX call.

  2. Use JSONP, which consists of inserting a script tag into the page to bypass the same-origin policy. This requires that the AJAX server wrap its JSON output in ?(...), where the URL includes callback=?. Then, as soon as a response has been received, start another request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文