What is cross domain? How do I solve cross-domain problems?

Source: Internet
Author: User
Tags sub domain

What is cross domain.

Cross-domain, refers to the browser can not execute other Web site script. It is caused by the browser's homology policy and is the security restrictions imposed by the browser .

The so-called homology refers to, domain name, protocol, port are the same, do not understand it doesn't matter, give a chestnut:

http://www.123.com/index.html Call http://www.123.com/server.php (non-Cross-domain)

http://www.123.com/index.html Call http://www.456.com/server.php (main domain name is different: 123/456, Cross-domain)

http://abc.123.com/index.html Call http://def.123.com/server.php (sub domain name is different: Abc/def, Cross-domain)

http://www.123.com:8080/index.html Call http://www.123.com:8081/server.php (port is different: 8080/8081, Cross-domain)

http://www.123.com/index.html call https://www.123.com/server.php (protocol different: Http/https, Cross-domain)

Note that localhost and 127.0.0.1 are both points to the native, but they are also cross-domain.

When the browser executes a JavaScript script, it checks which page the script belongs to and is not executed if it is not a homologous page.

Solution:

1, JSONP:

This is not the way to go, but note that JSONP only supports get requests and does not support post requests.

2. Agent:

For example www.123.com/index.html need to call www.456.com/server.php, you can write an interface www.123.com/server.php, which is called by this interface at the back end www.456.com/ Server.php and get the return value, and then return to Index.html, which is a proxy pattern. The equivalent of bypassing the browser side, naturally there is no cross-domain problem.

3, PHP side modified header (XHR2 way)

Add the following two sentences to the PHP interface script:

Header (' access-control-allow-origin:* ');//Allow all sources to access

Header (' Access-control-allow-method:post,get ');//The way to allow access

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.