JS three ways to achieve cross-domain access _javascript skills

Source: Internet
Author: User
Tags script tag

JavaScript cross-domain access is a common problem for web developers, what is Cross-domain, a script loaded on one domain Gets or operates a document property on another domain, and three implementations of JavaScript Cross-domain methods are listed below:
1. Cross-domain implementation based on IFRAME
cross-domain requirements based on IFRAME implementation The aa.xx.com,bb.xx.com feature is that two pages must belong to an underlying domain (for example, all xxx.com, or xxx.com.cn), using the same protocol (for example, all HTTP) and the same port (for example, all are 80) so that you can add document.domain to the two pages and then implement the function of the parent page calling the child page, as follows:
Page One:

 
 

Page Two:

 
 

At this time, the parent page can call the child page a function, to achieve the JS Cross-domain access
2. Implementing Cross-domain based on the script label
The script label itself has access to resources from other domains, is not limited by the browser's homology policy, and can be created dynamically by creating a script label on the page, as follows:

var script = document.createelement (' script '); 
SCRIPT.SRC = "Http://aa.xx.com/js/*.js"; 
Document.body.appendChild (script); 

So by dynamically creating a script tag can load other domain JS files, and then through this page can be called after the Load JS file function, the flaw is not to load other domain documents, can only be JS file, Jsonp is achieved by this way, Jsonp by passing in a callback parameter to another domain, wrapping the callback parameter value and the JSON string into a JavaScript function back through the background of the other domain, because the request is made through the script label, The browser will parse the returned string into JavaScript to implement the data transfer between the field and the domain.
The support for JSONP in jquery is also based on this scenario.
3. The Backstage proxy way
This approach solves all cross-domain problems, that is, the background as a proxy, every time a request to another domain is forwarded to the background of the domain, the background of the domain is accessed by simulating an HTTP request to the other domain, returning the returned results to the foreground, and the benefit is that both the document being accessed and the JS file can be implemented across the domain.

The above JS to achieve Cross-domain access to the three methods first to everyone, we carefully study and learn, there will be some harvest

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.