Jelle De Loecker
Jelle De Loecker

Reputation: 21955

Sending cross-domain data in IE6, 7, 8 & 9

I just found out (the hard way) that you can't make ajax calls to another domain.

I've read about iFrames (which only works in IE6) and a cross domain xml request (which only works from ie8 onwards)

So is there any other way? I basically just need to send data to another server, not receive it.

Upvotes: 2

Views: 1108

Answers (4)

duri
duri

Reputation: 15351

I'd combine <iframe> and the <form>'s ability to be sent even to another domain. Main file:

<iframe id=ifr src=form.html style="display: none;"></iframe>
<input id=send-me><input type=button onclick="senddata();" value=Send>
<script>
function senddata()
{
    var ifr = document.getElementById('ifr'),
        f = ifr.contentWindow.document.forms[0];
    f.elements.data.value = document.getElementById('send-me').value;
    f.submit();
}
</script>

form.html:

<form action="http://another.doma.in/" method=post>
    <input type=hidden name=data>
</form>

This will make POST HTTP request to another domain and send there the content of <input id=send-me>.

Please note that this is just basic proposal and will need adjustment if you want to for example send data multiple times without refresh.

Upvotes: 0

Jelle De Loecker
Jelle De Loecker

Reputation: 21955

I should have clarified: I did not want to use a (large) library, and can't use a proxy-script.

Basically I decided a GET would be "good enough" for what I was trying to do (track impressions)

So when the script gets triggered it creates an iframe, hides it and sets the source url to my script + the correct GET parameters. No forms needed.

Upvotes: 0

Joe
Joe

Reputation: 82624

JSONP might be what you need. there are plenty of examples out there. Here's a good one: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

Upvotes: 0

thescientist
thescientist

Reputation: 2948

sounds like you would need to use a server-side proxy script. i.e. an AJAX request to a (for example) PHP script which would make an HTTP/cURL request for you.

Upvotes: 2

Related Questions