How does PHP achieve cross-domain, PHP achieves cross-domain_PHP tutorial

WBOY
Release: 2016-07-12 08:50:58
Original
878 people have browsed it

How does PHP realize cross-domain, PHP realizes cross-domain

Due to work needs, some interfaces of the client software are implemented with HTML CSS Javascript. These files are placed locally on the client to It is loaded as a local file, but in some cases, it is necessary to connect to the server to obtain some information. If no processing is done, the request fails and the returned information 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 and blocks cross-domain requests. Just look at the HTTP request process. When the backend is requested for the first time, the browser realizes that it is accessing a cross-resource and does not directly send a GET request to obtain data. Instead, it sends an OPTIONS request to ask whether the resource can be accessed. We call it a Preflight request. By default, due to the existence of the same-origin policy, the header returned by this request does not have the 'Access-Control-Allow-Origin' attribute, so the access fails. If you want to achieve cross-domain, the key lies in the server, and the client code can be written in the normal way. For the server, you only need to add this attribute to the returned header information where the OPTIONS request is received. The code is as follows:

header("Access-Control-Allow-Origin: *");
Copy after login
Note that it must be set before all information is output to the client.

The above is the editor’s introduction to how PHP implements cross-domain content. I hope it will be helpful to everyone!

http://www.bkjia.com/PHPjc/1133044.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133044.htmlTechArticleHow does PHP realize cross-domain? Due to work needs, some interfaces of the client software use HTML CSS Javascript Implementation, these files are placed locally on the client and loaded as local files...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!