How to send data to remote server using Javascript

前端 未结 3 1372
栀梦
栀梦 2020-11-29 08:43

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

3条回答
  •  鱼传尺愫
    2020-11-29 09:07

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

    
    

    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.

提交回复
热议问题