开发者

With JavaScript Ajax polling for widget

开发者 https://www.devze.com 2023-01-26 05:30 出处:网络
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.

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开发者_C百科 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号