开发者

Cross domain with jQuery - on a subdomain

开发者 https://www.devze.com 2023-01-12 21:48 出处:网络
I have a iframe and want to either get some of the HTML from the page in the iframe or the opportun开发者_StackOverflow中文版ity to change the HTML on the page in the iframe with jQuery.

I have a iframe and want to either get some of the HTML from the page in the iframe or the opportun开发者_StackOverflow中文版ity to change the HTML on the page in the iframe with jQuery.

I use this code to get content from the iframe:

$('iframe#iframeBB').contents().find('table').each(function() {
 var at = $(this).attr('summary');
 if (at == "Latest Data") {
    $('table#main').html($(this).html());
 }
});

This works fine as long as the source for the iframe is at the same domain. My problem is that the source is on another subdomain. I know this is against the Same-Origin Policy, but is there a way to do this?


If it's a subdomain, you have to let both work on the same tld, by setting document.domain on your pages.

document.domain = "yourdomain.com"


I think that the only way to do is to get the html output of the page using server side and output it into the iframe.


The document.domain solution didn't worked for me. But I figured out, that you can allow cross domain access by server setting the Access-Control-Allow-Origin header.

You can do this e.g. in the .htaccess file if the server supports it:

  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "http://REQUESTING.domain"
  </IfModule>
0

精彩评论

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