制作一个AJAX请求到另一台服务器 [英] Making an AJAX request to another server

查看:401
本文介绍了制作一个AJAX请求到另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从W3Schools的,在那里如果U请求失败请求AJAX调用远程服务器一个AJAX样本code:

I have an AJAX sample code from W3Schools, where if U request an AJAX call to remote server the request fails:

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?

推荐答案

看起来你已经撞到了同源策略。你必须使用相对路径,而不是你的绝对 http://www.google.com 路径。

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.

作为一个可能的解决方法,你可以建立一个非常简单的反向代理(带的mod_proxy 如果你使用Apache)。这将允许您使用相对路径在你的AJAX请求,而HTTP服务器将作为任何远程位置的代理。

As one possible workaround, you could set up a very simple reverse proxy (with mod_proxy if 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.

基本配置指令设置一个反向代理在mod_proxy的是增强了ProxyPass。您通常会使用它,如下所示:

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:

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

在这种情况下,浏览器将请求 /web-services/service.xml ,但服务器将通过作为代理 http://third-party.com/web-services/service.xml 。

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.

另一种常见的解决方法是使用 JSONP

Another common workaround would be to use JSONP.

这篇关于制作一个AJAX请求到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆