In development, multiple domain names, multiple services, access to resources under different domain names, or request interfaces under different domain names are often reported as OPTIONS 403 error. To solve this problem, you can add the following code to the entry file of each project (PHP) :
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS");
header("Access-Control-Allow-Headers:*");
Copy the code
Nginx is a Web server that uses location / {} as the source of the Web server.
location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers '*'; if ($request_method = 'OPTIONS') { return 204; }}Copy the code
OPTIONS handshake requests will return 204 status and will not prevent POST requests from continuing.
[The following is added later]
After that, some models of Vivo and OPpo have the problem that the request header is not supported. The final solution is to set access-Control-allow-headers to the allowed request header field instead of *, as follows:
add_header Access-Control-Allow-Headers 'Origin, X-Requested-With, Content-Type, Accept, Platform, Token';
Copy the code