使用 web 代理实现 Ajax 跨域调用
来源:互联网 发布:阿里云 自定义端口 编辑:程序博客网 时间:2024/06/07 12:10
JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
The XMLHttpRequest object (also known as the XMLHTTP object in Internet Explorer) is at the core of today's most exciting AJAX web applications. But actually writing client web applications that use this object can be tricky given restrictions imposed by web browsers on network connections across domains. This HOWTO provides describes the issue in simple, easy to understand language and provides one possible solution: a web proxy that relays network requests from your web server to services such as the Yahoo! Web Service APIs.
- Why You Need a Proxy
- PHP Proxy for Yahoo! Web Services
- Other Solutions
- For More Information
Why You Need a Proxy
All modern web browsers impose a security restriction on network connections, which includes calls to XMLHttpRequest. This restriction prevents a script or application from making a connection to any web server other than the one the web page originally came from (Internet Explorer will allow cross-domain requests if the option has been enabled in the preferences). If both your web application and the XML data that application uses come directly from the same server, then you do not run into this restriction.
If, however, you serve your web application from one web server and you make web service data requests to another server -- for example, to the Yahoo! Web Services -- then the browser prevents the connection from being opened at all. Bummer.
There are a number of solutions to this problem but the most commonly-used one is to install a proxy on your web server. Instead of making your XMLHttpRequest calls directly to the web service, you make your calls to your web server proxy. The proxy then passes the call onto the web service and in return passes the data back to your client application. Because the connection is made to your server, and the data comes back from your server, the browser has nothing to complain about.
For security reasons it's a good idea for any proxy you install on your web server should be limited in use. An open proxy that passes on connections to any web site URL is open to abuse. Although it is difficult to limit the connections to your proxy from only your application, you can prevent the proxy from making connections to servers other than those you specify. Hard code the URL to connect to in the proxy itself or provide limited options. This makes the proxy less open and less useful to users other than your client application.
PHP Proxy for Yahoo! Web Services
For the Yahoo! Developer Network JavaScript Developer Center we have provided sample code for a simple web proxy, written in PHP, that takes requests for the Yahoo! Search APIs. You can install this proxy on your own web server in any convenient location (your web server must be set up to run PHP).
The proxy encodes the Yahoo! Web services site URL in a global variable called HOSTNAME. ou will need to modify this variable to refer to the Yahoo! Web Services API you'll be using. This is the domain used by the Yahoo! Search web services; other domains include Yahoo! Local (http://local.yahooapis.com
) and Yahoo! Travel (http://api.travel.yahoo.com
).
define ('HOSTNAME', 'http://search.yahooapis.com/');
To use the PHP web proxy in your client application, the URL for the request in the JavaScript code includes the path for the Yahoo! Web Services request, minus the domain name. The domain name is added by the proxy itself on the server side. This code snippet comes from a more complete XMLHttpRequest code sample on our JavaScript Developer Center.
// The web services request minus the domain name
var path = 'VideoSearchService/V1/videoSearch?appid=YahooDemo&query=madonna&results=2';
// The full path to the PHP proxy
var url = 'http://localhost/php_proxy_simple.php?yws_path=' + encodeURIComponent(path);
... // core xmlhttp code
xmlhttp.open('GET', url, true);
Note that although this example uses an HTTP GET request, the sample PHP web proxy also supports POST.
You could modify the proxy to do post-processing of the data you get from the request on the server side, for example, to strip out only the elements you're interested in or the parse the XML into a format you can more comfortably handle in JavaScript.
Other Solutions
In addition to using a web proxy to pass web services data to your application, there are several other options to working around cross-domain browser restrictions:
- Use apache's
mod_rewrite
ormod_proxy
to pass requests from your server to some other server. In your client code you just make the request as if it was actually on your server -- no browser problems with that. Apache then does its magic and makes the request to the other server for you. - Use JSON and dynamic
<script>
tags instead of XML and XMLHttpRequest. You can get around the browser security problem altogether by making your web services request directly inside a<script>
tag. If the Yahoo! Web Service you're using can output JSON (using theoutput=json
andcallback=
function parameters), the data you get back from the web service is evaluated as a JavaScript object when the page is loaded. See our JSON Documentation for an example of how to do this in your own scripts. - Digitally sign your scripts. In Firefox you can apply a digital signature to your script and those scripts will then be considered "trusted" by the browser. Firefox will then let you make XMLHttpRequests to any domain. However, no other browsers support script signing at this time, so this solution is of limited use.
For More Information
For more information on JavaScript, XMLHttpRequest, Yahoo! Web Services APIs and other JavaScript development topics, see The Yahoo! Developer Network JavaScript Developer Center.
- 使用 web 代理实现 Ajax 跨域调用
- 使用 web 代理实现 Ajax 跨域调用
- jsp代理实现ajax跨域调用获取数据
- 使用proxy.jsp代理实现ajax跨域请求
- Python Web 实现Ajax调用
- 使用 AJAX 调用 Web 服务
- {{js跨域请求解决方案}}Use a Web Proxy for Cross-Domain XMLHttpRequest Calls(使用服务器端代理实现Ajax跨域请求)
- jsp实现 AJAX 跨域代理
- ajax跨域之---服务器端代理实现
- 使用 jQuery 集成的 $.ajax 实现 JSONP 跨域调用
- JQuery调用Web service实现AJAX原来如此?
- 使用 AJAX 调用 SOAP Web 服务
- 使用 AJAX 调用 SOAP Web 服务
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(4):异步通讯层生成的客户端代理类、使用HTTP GET进行调用
- Jquery Ajax GetJson实现跨域调用
- Ajax跨域调用WCF的实现
- AJAX跨域访问通信代理,java中的实现
- 用Squid来实现Ajax跨域代理
- Nginx数据结构及相关接口函数
- 用接口,多态,继承,类计算三角形和矩形的周长和面积
- hdu 1856 More is better
- oracle 隐式游标,显示游标,游标循环,动态SELECT语句和动态游标,异常处理,自定义异常,
- hdu1031 Design T-Shirt
- 使用 web 代理实现 Ajax 跨域调用
- 模态窗口刷新、清除缓存
- inline-block
- linux下的基本网络配置
- DOS下检索XHCI下所有的USB设备,并显示出设备接在哪个端口上,以及该USB device的speed。
- 通过应用实例讨论Java多态的实现
- android内部存储器和外部存储器
- gcc static静态编译选项提示错误修正(/usr/lib/ld: cannot find -lc)
- 线性结构的特点