JavaScript cross-Domain access solution

Source: Internet
Author: User

Because of security considerations, JavaScript is limited to cross-domain access, but sometimes we want to be able to do some reasonable cross-domain access things, so what to do?

There are two types of situations:

One, access to the page between subdomains based on the same parent domain

See the following 3 domain domains:

1、taobao.com
   2、jipiao.taobao.com
   3、promotion.taobao.com

They have the same parent domain taobao.com

Second, based on the access between different parent domain pages

See the following 3 domain domains:

1、taobao.com
   2、baidu.com

3, sina.com.cn

They have different parent domains.

Scenarios for resolving cross-domain between them:

<!--[if!supportlists]-->①<!--[endif]--> Server proxy: Domain A's page JS needs access to the link under Domain B to obtain data, which establishes a proxy program on the server side of Domain A ( May be ASP, servlet, and so on any server-side program, domain A page JS directly call this domain proxy program, proxy program is responsible for the request sent to the link under the domain B and get the data, and finally through proxy to return the data to the page JS use.

After the access process is: Domain A under the Js--à domain A proxy---à domain B under the link

Example:

First step:

JavaScript script on domain a:http://jipiao.taobao.com/test.htm page

<script type=”text/javascript”>
  Var sUrl=” http://Jipiao.taobao.com/proxy.do”; //本域下代理地址
var callback =
{
  success: function(res) { alert(res.responseText); },
  failure: function(res) { alert(‘failure’);},
  argument:{}
}
  YAHOO.util.Connect.asyncRequest(’GET’, sUrl, callback, null);
</script>

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.