views:

164

answers:

2

I'm trying to send chunks of data from many different servers my app is on, to mine.
Using some dummy image source, passing my data as a GET query. (img.gif?aaa=xxx&bb=yyy...)
the Query is many times too long and gets cut.

is there some better way for me to send the data cross-browser?

A: 

It would be the best if you used POST method when sending the data.

 var msgSender = new ActiveXObject("Microsoft.XMLHTTP"); 
 msgSender.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 msgSender.setRequestHeader("Encoding", "Windows-1257")     
 msgSender.open("POST", "http://yourderver/page" ,true);
 msgSender.onreadystatechange = function(){...};
 var msg = "your very long message goes here";

 //preparing post data
 var strToSend = "someotherarg=somevalue" + username;
     strToSend+= "&msg=" + msg;
 strToSend = escape(strToSend);
 msgSender.send(strToSend);

The solution is even easier, if you use jQuery - just call $.post() method: http://docs.jquery.com/Ajax/jQuery.post

EDIT: However, this will not work cross-domain, unless you specify 'Access-Control' headers on your server and the clients have modern enouhg browsers (FireFox 3.5+ etc)

So, another solution is to include a hidden IFRAME in your page (the page lives on your server then) which contains a form and you call Submit() of that form to POST the data.

naivists
hidden iframe? and how would the data go to iframe if the query is too long? anyway, that would be 2 HTTP requests each time...
vsync
If the iframe is from your domain, you can access its DOM using javascript! `document.getElementById("yourframe").document.forms[0].submit()` etc!
naivists
I know, but as I said already, the data is crossed-domain. this is it. iframe trick won't help a bit..
vsync
It is not a problem to submit a normal HTML form to a different domain than yours! Or... don't I get the problem?
naivists
so why did you said in your edit "the page lives on your server "... I like your iframe idea :)
vsync
I said it because - if you had your "base page" on `http://server1` and made an `<iframe src='http://server2'>`, then you could not access the iframe from javaScript due to cross-domain restrictions
naivists
A: 

Split your payload (e.g. at 1024 bytes), then send using several GET requests.

Piskvor
That's what I thought doing, although it seems a bit of an ugly solution to me :/
vsync
Ugly indeed; but with AJAX POST, the [Same Origin Policy][1] will block you; and although [RFC2616][2] says "The HTTP protocol does not place any a priori limit on the length of a URI", [some user agents][3] are pretty sure that 2083 bytes ought to be enough for everybody. [1]: http://en.wikipedia.org/wiki/Same_origin_policy [2]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1 [3]: http://support.microsoft.com/kb/208427
Piskvor