Javascript 向另一台服务器发出 AJAX 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2851164/
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
Making an AJAX request to another server
提问by santhosh
I have AJAX code where if you request an AJAX call to remote server the request fails:
我有 AJAX 代码,如果您请求对远程服务器进行 AJAX 调用,请求将失败:
function loadXMLDoc() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "http://www.google.com", true);
xmlhttp.send();
}
What can I do to solve this?
我能做些什么来解决这个问题?
回答by Daniel Vassallo
It looks like you have bumped into the same origin policy. You have to use a relative path instead of your absolute http://www.google.compath.
看起来您遇到了同源策略。您必须使用相对路径而不是绝对http://www.google.com路径。
As one possible workaround, you could set up a very simple reverse proxy(with mod_proxyif you are using Apache). This would allow you to use relative paths in your AJAX request, while the HTTP server would be acting as a proxy to any "remote" location.
作为一种可能的解决方法,您可以设置一个非常简单的反向代理(如果您使用的是 Apache,则使用mod_proxy)。这将允许您在 AJAX 请求中使用相对路径,而 HTTP 服务器将充当任何“远程”位置的代理。
The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:
在 mod_proxy 中设置反向代理的基本配置指令是 ProxyPass。您通常会按如下方式使用它:
ProxyPass /web-services/ http://third-party.com/web-services/
In this case, the browser would be requesting /web-services/service.xmlbut the server would serve this by acting as a proxy to http://third-party.com/web-services/service.xml.
在这种情况下,浏览器将/web-services/service.xml发出请求,但服务器将通过充当 的代理来提供服务http://third-party.com/web-services/service.xml。
Another common workaround would be to use JSONP.
另一种常见的解决方法是使用JSONP。
回答by Justin Ethier
As a security measure, AJAX does not allow you to make requests to other domains. Cross Domain Ajax: a Quick Summarydiscusses several ways to work around the problem. The easiest way is to use your server as a proxy to download remote content:
作为一项安全措施,AJAX 不允许您向其他域发出请求。跨域 Ajax:快速摘要讨论了解决该问题的几种方法。最简单的方法是使用您的服务器作为代理下载远程内容:
This is one of the most common approaches. Your script calls your server, your server makes the call to the remote server and then returns the result back to the client. There are some definite advantages to this approach: you have more control over the entire lifecycle. You can parse the data from the remote server, do with it what you will before sending it back to the client. If anything fails along the way, you can handle it in your own way. And lastly, you can log all remote calls. WIth that you can track success, failure and popularity.
这是最常见的方法之一。您的脚本调用您的服务器,您的服务器调用远程服务器,然后将结果返回给客户端。这种方法有一些明显的优势:您可以更好地控制整个生命周期。您可以解析来自远程服务器的数据,在将其发送回客户端之前对其进行处理。如果在此过程中出现任何故障,您可以按照自己的方式处理。最后,您可以记录所有远程呼叫。有了它,您可以跟踪成功、失败和受欢迎程度。
回答by Dave
You can use dynamic script loading. Here is an article that I wrote about it.
您可以使用动态脚本加载。这是我写的一篇文章。
回答by Joseph Astrahan
I wanted to post another variation on the top answer. I tried to use ProxyPassin my .htaccess file but I kept getting Internal Service Errors. Finally after some reading I discovered there was another way to do this with the rewrite engine.
我想在最佳答案上发布另一个变体。我试图ProxyPass在我的 .htaccess 文件中使用,但我一直收到内部服务错误。最后,经过一些阅读,我发现使用重写引擎还有另一种方法可以做到这一点。
RewriteEngine On
RewriteRule ^mail.php$ http://otherwebsite.com/mail.php [P,L]
The P in [P,L]tells the rewrite system that its using mod_proxy. This worked for me and I didn't get internal server errors.
P in[P,L]告诉重写系统它使用mod_proxy. 这对我有用,我没有收到内部服务器错误。
The advantage to this approach is since it's using the Rewrite Engine, you have more control over the variables you might need to dynamically send to the script.
这种方法的优点是因为它使用Rewrite Engine,您可以更好地控制可能需要动态发送到脚本的变量。

