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!