如何为XMLHTTPRequest指定外部网站

如何为XMLHTTPRequest指定外部网站

本文介绍了如何为XMLHTTPRequest指定外部网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中使用XMLHTTPRequest时,我想将其发送到外部网站,而不是托管.js文件的网站。要将它发送到当前服务器上的test.php,我会使用

  request.open(POST,test。 php,true); 

但是对于第二个争论,如何将其发送到另一个网站。 example.com/test.php在当前服务器上查找文件,并看起来完全失败。

解决方案

出于安全原因,你不能这样做。请参阅JavaScript的。





最好的办法是拥有一个接收Ajax请求的服务器端代理反过来,将HTTP请求发送到其他服务器。这应该通过清理输入并将发送的请求类型和联系的服务器列入白名单来仔细实现。


When using an XMLHTTPRequest in javascript, I want to send it to an external website, rather than the one where the .js file is hosted. To send it to test.php on the current server, I would use

request.open("POST", "test.php", true);

but for the second arguemnt, how do I send it to another website. "example.com/test.php" looks for a file on the current server, and "http://example.com/test.php" justseems to outright fail.

解决方案

You can't for security reasons. See the same origin policy for JavaScript.

There are some workarounds that exploit browser bugs or corner cases, but using them is not recommended.

The best approach is having a server-side proxy that receives Ajax requests, and in turn, sends HTTP requests to other servers. This should be carefully implemented by sanitizing input and whitelisting the types of requests that are sent, and the servers that are contacted.

这篇关于如何为XMLHTTPRequest指定外部网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:03