Monday, May 20, 2024
74
rated 0 times [  79] [ 5]  / answers: 1 / hits: 72433  / 14 Years ago, sat, february 19, 2011, 12:00:00

I need to send data to a remote server using javascript. How do I do this?



Background info:
There's a webpage from which I extract some information using JS, and I need to send it back to another server for processing. Response is not neccesary. The data is XML, Which I've URLencode'd.



How would one do this?



EDIT



The server I'm requesting the data from is not the same that receives the data. Just to clarify.


More From » remote-server

 Answers
70

One of the most common ways to do this is AJAX. Here's how you perform an AJAX post request using jQuery:



<script type=text/javascript>
$.post('/remote-url', {xml: yourXMLString });
</script>


On the server side you process it like any other POST request. If you're using PHP it's $xml = $_POST['xml'];



The biggest limitation of AJAX is that you're only allowed to make requests to the same domain the document has been loaded from (aka cross-domain policy). There are various ways to overcome this limitation, one of the easiest one is JSONP.






UPD. For cross-domain requests an extremely simple (though not universal) solution would be:



(new Image).src = 'http://example.com/save-xml?xml=' + escape(yourXMLString)


This will issue a GET request (which cannot exceed 2KB in Internet Explorer). If you absolutely need a POST request or support for larger request bodies you can either use an intermediate server-side script on your domain or you can post a dynamically created html form to iframe.


[#93671] Thursday, February 17, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marisela

Total Points: 103
Total Questions: 105
Total Answers: 102

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;