15

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.

5
  • Use XMLHttpRequest - you also need to check cross-domain policy? Commented Feb 19, 2011 at 8:07
  • What kind of endpoint do you have on the remote server? Can it communicate over HTTP? Commented Feb 19, 2011 at 8:11
  • What do You mean by this? If what Youre asking is, does it have a webserver on it, then Yes, it does! Commented Feb 19, 2011 at 8:17
  • javaScript compulsory ? why not jQuery ? Commented Feb 19, 2011 at 9:56
  • 1
    jQuery IS JavaScript... And jQuery has no better luck doing cross domain access than plain Ajax Commented Feb 19, 2011 at 12:20

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Please read the question again. I explicitly pointed out, that it was a remote server, meaning, that it is not the same that the page is loaded from.
@Janis Please make yourself familiar with what remote server is. Remote server is a "computer that is not attached to a user's keyboard but over which he or she has some degree of control, whether it is in the same room, another part of a building, or another part of the world. Also called remote computer or remote host". Btw, I addressed cross-domain requests in my answer.
I think here JSONP would work like the image. Since there is no need for a return, the image should be enough. +1 for the update and the (new Image).src syntax.
6
  • submit a form using POST. That is working on all browsers cross domains. Have the server process the post. the form can be submitted to a hidden frame if you want to simulate AJAX
  • Use Cross Domain Resource Sharing (MDC) (IE XDR)
  • use a web bug (create an image, set the source to the url you want - smallish GET requests only)

    var img = new Image();
    img.src="http://www.otherserver.com/getxml?xml="+encodeURIComponent(yourXML); (Oops, I see Lebedev did more or less the same in his update)

  • use a proxy, i.e. have your server talk to the other server for you

Comments

0

Look into Javascript's XMLHTTPRequest method -- or start with a Google search for AJAX. There's lots of ways to do this -- including some very easy ways through JS libraries like jQuery -- but a more specific answer would require some more specifics on the specific technologies you're using.

EDIT: You can set up the AJAX request to post to a server-side script (acting as a proxy) on your own domain, and have that script turn around and post the data to your remote server.

1 Comment

Wouldn't there be cross server issue ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.