PHP server side handles cross-domain issues

藏色散人
Release: 2023-04-07 22:20:01
forward
2375 people have browsed it

Related recommendations: "PHP Tutorial"

At first, I only set

header('Access-Control-Allow-Origin:*');
Copy after login

at the beginning of the file and then reported an error

Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers in preflight response.
Copy after login

Solution:

Settings at the beginning of the file

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:OPTIONS, GET, POST'); // 允许option,get,post请求
header('Access-Control-Allow-Headers:x-requested-with'); // 允许x-requested-with请求头
后成功
Copy after login

The more confusing one is the setting of Access-Control-Allow-Headers. After reading this document, I understand

If the browser request includes the Access-Control-Request-Headers field, the Access-Control-Allow-Headers field is required. It is also a comma-separated string indicating all header fields supported by the server, not limited to the fields requested by the browser in "preflight".

PHP server side handles cross-domain issues

Then I checked that the request header indeed included Access-Control-Request-Headers

Then the interface information was successfully returned

PHP server side handles cross-domain issues

PHP server side handles cross-domain issues

The above is the detailed content of PHP server side handles cross-domain issues. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
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!