我有一个从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", "www.google", true); xmlhttp.send(); }
我能做些什么来解决这个?
What can I do to solve this?
推荐答案看起来你已经撞到了同源策略。你必须使用相对路径,而不是你的绝对 www.google 路径。
It looks like you have bumped into the same origin policy. You have to use a relative path instead of your absolute www.google 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/ third-party/web-services/在这种情况下,浏览器将请求 /web-services/service.xml ,但服务器将通过作为代理 third-party/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 third-party/web-services/service.xml.
另一种常见的解决方法是使用 JSONP 。
Another common workaround would be to use JSONP.
更多推荐
制作一个AJAX请求到另一台服务器
发布评论