Answer the question
In order to leave comments, you need to log in
Access-Control-Allow-Origin, why doesn't the browser understand it?
Script code:
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Expose-Headers: *");
header("Content-type: application/json");
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
exit;
}
POST http://127.0.0.1/?/auth/login
{password: "123456", phone: "79999999999"}
Access to XMLHttpRequest at 'http://127.0.0.1/?/auth/login' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
access-control-allow-headers: *
access-control-allow-methods: *
access-control-allow-origin: *
access-control-expose-headers: *
connection: keep-alive
content-type: application/json
date: Wed, 09 Jun 2021 15:29:11 GMT
server: nginx/1.19.10
transfer-encoding: chunked
Answer the question
In order to leave comments, you need to log in
The problem was solved by adding a rule to default.conf in location
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' '*';
add_header 'Access-Control-Expose-Headers' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question