I have an Arduino server that runs on IP 192.168.0.177. If you go to this address, it returns data in JSON format. For example, in a React application, I get this JSON something like this:
const [data, setData] = useState([])
axios.get('http://192.168.0.177')
.then((res) => setData(res.data))
.catch((err) => console.log(err))
In the Arduino sketch itself part of the code is like this:
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Access-Control-Allow-Methods: GET");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println("[");
client.println("{ \"id\": 1, \"name\": \"John\" }, ");
client.println("{ \"id\": 2, \"name\": \"Paul\" } ");
client.println("]");
If you run the React app on a local network, everything works fine. Localhost has http, not https, and it sends a request to the http server, so everything works.
The problem already appeared when I launched my React app on the global network. In other words, the address is something like this: https://app.com. Now if I send a request to the Arduino via https, the console writes an error that you can't send a request from https to http. Then I changed the query in the React code, or rather added only the 's'symbol:
axios.get('https://192.168.0.177')
^
Then there was another problem related to CORS. The console displays this error:
Request from an external source is blocked: the single source Policy prohibits reading a remote resource on https://192.168.0.177/. (Reason: the CORS request failed).
I read all about CORS and everywhere they write that you need to add the header "Access-Control-Allow-Origin: *"to the server. But it was in my Arduino code all the time.
I'm sorry, I'm not very good at this, so I'm asking for help. Where did I make a mistake?