简体   繁体   English

向另一台服务器发出AJAX请求

[英]Making an AJAX request to another server

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? 我该怎么做才能解决这个问题?

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.com path. 您必须使用相对路径而不是绝对的http://www.google.com路径。

As one possible workaround, you could set up a very simple reverse proxy (with mod_proxy if you are using Apache). 作为一种可能的解决方法,您可以设置一个非常简单的反向代理 (如果您使用的是Apache,则使用mod_proxy )。 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. 这将允许您在AJAX请求中使用相对路径,而HTTP服务器将充当任何“远程”位置的代理。

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. 在mod_proxy中设置反向代理的基本配置指令是ProxyPass。 You would typically use it as follows: 您通常会按如下方式使用它:

ProxyPass     /web-services/     http://third-party.com/web-services/

In this case, the browser would be requesting /web-services/service.xml but 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

As a security measure, AJAX does not allow you to make requests to other domains. 作为安全措施,AJAX不允许您向其他域发出请求。 Cross Domain Ajax: a Quick Summary discusses several ways to work around the problem. 跨域Ajax:快速摘要讨论了解决该问题的几种方法。 The easiest way is to use your server as a proxy to download remote content: 最简单的方法是使用您的服务器作为代理来下载远程内容:

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. 这样你就可以追踪成功,失败和受欢迎程度。

I wanted to post another variation on the top answer. 我想在最佳答案上发布另一个变体。 I tried to use ProxyPass in my .htaccess file but I kept getting Internal Service Errors. 我试图在我的.htaccess文件中使用ProxyPass ,但我一直收到内部服务错误。 Finally after some reading I discovered there was another way to do this with the rewrite engine. 经过一些阅读后,我发现还有另一种方法可以使用重写引擎。

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 . [P,L]中的[P,L]告诉重写系统它使用mod_proxy This worked for me and I didn't get internal server errors. 这对我有用,我没有得到内部服务器错误。

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 ,所以您可以更好地控制动态发送到脚本所需的变量。

You can use dynamic script loading . 您可以使用动态脚本加载 Here is an article that I wrote about it. 这是我写的关于它的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM