Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Subdomain tunneling with jQuery and document.domain
/**
* Replace $.ajax on your subdomain with a copy taken
* from your base domain. All jQuery AJAX actions go
* through $.ajax (i.e. $.get, $.post), so it's all good.
*/
(function() {
var iframe,
onload,
queue = [],
// This has to be set both here and in iframe.html
document.domain = 'example.com';
// Back up this page's copy of $.ajax
window.$._ajax = window.$.ajax;
// We'll first replace $.ajax with a thin wrapper that both
// loads our iframe tunnel and saves invocations until the
// iframe is ready
$.ajax = function(params) {
if (!iframe) {
// tunnel.html should be a bare bones html page that
// includes a copy of jQuery, and sets document.domain
// to 'example.com'
iframe = $('<iframe>')
.attr('src', 'http://example.com/tunnel.html')
.load(onload)
.appendTo('head')[0];
}
// Save calls to $.ajax, execute when we're ready
queue.push(params);
};
function onload() {
// Our prize: a version of $.ajax that can communicate safely
// with our base domain
window.$.ajax = iframe.contentWindow.jQuery.ajax;
// Flush queued $.ajax calls
$.each(queue, function(_, params) {
$.ajax(params);
});
queue = null;
}
})();
@ryanneufeld

This comment has been minimized.

Show comment Hide comment
@ryanneufeld

ryanneufeld Apr 9, 2012

This seems to break deferreds. Any suggestions on how someone would use them with this?

This seems to break deferreds. Any suggestions on how someone would use them with this?

@benvinegar

This comment has been minimized.

Show comment Hide comment
@benvinegar

benvinegar Apr 9, 2012

I actually haven't used deferreds with this code, so I'm not familiar with the trouble you're having. Normally I'd poke around and figure it out, but I'm kind of swamped at the moment.

Owner

benvinegar commented Apr 9, 2012

I actually haven't used deferreds with this code, so I'm not familiar with the trouble you're having. Normally I'd poke around and figure it out, but I'm kind of swamped at the moment.

@ryanneufeld

This comment has been minimized.

Show comment Hide comment
@ryanneufeld

ryanneufeld Apr 9, 2012

Thanks anyway. I can't seem to get this working in chrome. I'm stilling getting "Access-Control-Allow-Origin" errors. Does the iframe have to be served from the domain you're trying access?

Thanks anyway. I can't seem to get this working in chrome. I'm stilling getting "Access-Control-Allow-Origin" errors. Does the iframe have to be served from the domain you're trying access?

@benvinegar

This comment has been minimized.

Show comment Hide comment
@benvinegar

benvinegar Apr 9, 2012

Does the iframe have to be served from the domain you're trying access?

Yes, definitely. Did you read the original article? http://benv.ca/2011/3/7/subdomain-tunneling-with-jquery-and-document-domain/

Owner

benvinegar commented Apr 9, 2012

Does the iframe have to be served from the domain you're trying access?

Yes, definitely. Did you read the original article? http://benv.ca/2011/3/7/subdomain-tunneling-with-jquery-and-document-domain/

@ryanneufeld

This comment has been minimized.

Show comment Hide comment
@ryanneufeld

ryanneufeld Apr 9, 2012

yeah, sorry I mean sub-domain. But looks like no matter what I try, doing POST to the subdomain isn't going to work for me.

Since we're not using https I'll just switch to jsonp and GET.

yeah, sorry I mean sub-domain. But looks like no matter what I try, doing POST to the subdomain isn't going to work for me.

Since we're not using https I'll just switch to jsonp and GET.

@ryanneufeld

This comment has been minimized.

Show comment Hide comment
@ryanneufeld

ryanneufeld Apr 9, 2012

I have it backwards, I have code on my primary domain, that needs to post to the subdomain.

I have it backwards, I have code on my primary domain, that needs to post to the subdomain.

@ferdyh

This comment has been minimized.

Show comment Hide comment
@ferdyh

ferdyh Nov 1, 2012

IE doesn't seem to like this solution... Have you experienced any trouble using IE?

ferdyh commented Nov 1, 2012

IE doesn't seem to like this solution... Have you experienced any trouble using IE?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment