简体   繁体   中英

Cross subdomain ajax longpolling

I am creating a notifications script that check a database for changes and returns then in a javascript custom popup.

I have successfully made the jquery ajax loading and processing script, and php long polling page. all this worked great aside from the fact the long lasting request prevented any other ajax loaded content from working. I found that the way to stop this was to move it onto a different subdomain. however this causes issues with the js "Same Origin Policy", all the possible ways of doing this seem to not work with long connection speeds or the other option php proxy defeats the whole point in seperate domains.

Does anyone have any ideas how to do this, or any help at all.

Thanks

Oli

There is no way for solving this in a easy way.... the easiest way that I found is the following:

Create an IFrame (yes I know), this IFrame has to load a page coming from the subdomain (sub.example.com/base)

Both pages the main and the one loaded in the IFrame has to set the document.domain to '.example.com' after this you would be able to

  1. Make the long polling from the IFrame without occupiying one of the two domains connection
  2. Communicate between the main page and the IFrame, so you can just leave the IFrame for long-polling and then call a callback to the parent

This also solves another problem that you would have later that is the browser loding spinning wheel spinning until the first long poll is finished....

Another thing you could try is padded json. You can create a dynamic script at some url which accepts GET parameters, like sub.domain.com/jsonp.php?var=value which returns something like:

var response={a:1,b:2}

And you could dynamically load a <script/> element with that url as the source. The variable set in the jsonp will be accessible to other scripts, and is not subject to SOP.

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