How to send data to remote server using Javascript

折月煮酒 提交于 2019-11-27 04:39:48

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.

  • 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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!