Cross-origin Ajax call using Web Proxy

Source: Internet
Author: User
Javascript: Use a web proxy for cross-domain XMLHttpRequest CILS

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 internal SS 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
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 has des callto 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 calldirectly to the Web service, you make your callto 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 shocould 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 contains des 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 cocould 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'smod_rewriteOrmod_proxyTo
    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<script>Tag. If the Yahoo! Web
    Service you're using can output JSON (usingoutput=jsonAndcallback=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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.