Javascript 如何使用Javascript将数据发送到远程服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5049635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send data to remote server using Javascript
提问by Janis Peisenieks
I need to send data to a remote server using javascript. How do I do this?
我需要使用 javascript 将数据发送到远程服务器。我该怎么做呢?
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.
背景信息:有一个网页,我使用JS从中提取了一些信息,我需要将其发送回另一台服务器进行处理。响应不是必需的。数据是 XML,我已经 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.
我请求数据的服务器与接收数据的服务器不同。只是为了澄清。
回答by Alexey Lebedev
One of the most common ways to do this is AJAX. Here's how you perform an AJAX post request using jQuery:
执行此操作的最常见方法之一是 AJAX。以下是使用 jQuery 执行 AJAX 发布请求的方法:
<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'];
在服务器端,您可以像处理任何其他 POST 请求一样处理它。如果您使用的是 PHP$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.
AJAX 的最大限制是您只能向加载文档的同一个域发出请求(也就是跨域策略)。有多种方法可以克服此限制,其中一种最简单的方法是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.
这将发出 GET 请求(在 Internet Explorer 中不能超过 2KB)。如果您绝对需要 POST 请求或支持更大的请求正文,您可以在您的域中使用中间服务器端脚本,也可以将动态创建的 html 表单发布到 iframe。
回答by mplungjan
- 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
回答by Matt Howell
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.
查看 Javascript 的 XMLHTTPRequest 方法——或者从 Google 搜索 AJAX 开始。有很多方法可以做到这一点——包括一些通过 JS 库(如 jQuery)的非常简单的方法——但是更具体的答案需要关于您正在使用的特定技术的更多细节。
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.
编辑:您可以设置 AJAX 请求以发布到您自己域上的服务器端脚本(充当代理),然后让该脚本转向并将数据发布到您的远程服务器。