How PHP implements cross-domain _php instances

Source: Internet
Author: User
Due to work needs, some interface of the client software is implemented with Html+css+javascript, these files are placed locally on the client and loaded locally, but in some cases the connection server is required to obtain some information, and if no processing is done, the request fails and the information returned is as follows:

No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' null ' is therefore not allowed access.

This is because the browser's cross-domain policy works, blocking cross-domain requests. Look at the HTTP request process and know it. At the end of the first request, the browser realizes that it is accessing a cross with a resource, not directly sending a GET request to get the data, but instead sends an options request asking if the resource can be accessed. We call it a preflight request, and by default the request returns a header without the ' access-control-allow-origin ' attribute because of the presence of the same origin policy, so access fails.
If you want to implement cross-domain, the key is the server, the client's code is written in a normal way. For the server, simply add the attribute to the header information returned in the place where you received the options request, with the following code:

Header ("Access-control-allow-origin: *");

Note that it is important to set before all information is output to the client.

The above is a small part of the introduction of PHP How to achieve cross-domain related content, we hope to help!

  • 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.