制作一个AJAX请求到另一台服务器

编程入门 行业动态 更新时间:2024-10-22 07:32:22
本文介绍了制作一个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", "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请求到另一台服务器

本文发布于:2023-11-28 13:06:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:另一台   服务器   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!