title: Getting JSON from the Arduino server. Problems with CORS
body: <p>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:</p>
  <pre><code>const [data, setData] = useState([])
  axios.get('http://192.168.0.177')
    .then((res) =&gt; setData(res.data))
    .catch((err) =&gt; console.log(err))
  </code></pre>
  <p>In the Arduino sketch itself part of the code is like this:</p>
  <pre><code>client.println(&quot;HTTP/1.1 200 OK&quot;);
  client.println(&quot;Access-Control-Allow-Origin: *&quot;);
  client.println(&quot;Access-Control-Allow-Methods: GET&quot;);
  client.println(&quot;Content-Type: application/json&quot;);
  client.println(&quot;Connection: close&quot;);
  client.println();
  
  client.println(&quot;[&quot;);
  client.println(&quot;{ \&quot;id\&quot;: 1, \&quot;name\&quot;: \&quot;John\&quot; }, &quot;);
  client.println(&quot;{ \&quot;id\&quot;: 2, \&quot;name\&quot;: \&quot;Paul\&quot; } &quot;);
  client.println(&quot;]&quot;);
  </code></pre>
  <p>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.</p>
  <p>The problem already appeared when I launched my React app on the global network. In other words, the address is something like this: <a href="https://app.com" rel="nofollow noreferrer">https://app.com</a>. 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:</p>
  <pre><code>axios.get('https://192.168.0.177')
                 ^
  </code></pre>
  <p>Then there was another problem related to CORS. The console displays this error:</p>
  <blockquote>
  <p>Request from an external source is blocked: the single source Policy
  prohibits reading a remote resource on <a href="https://192.168.0.177/" rel="nofollow noreferrer">https://192.168.0.177/</a>.
  (Reason: the CORS request failed).</p>
  </blockquote>
  <p>I read all about CORS and everywhere they write that you need to add the header &quot;Access-Control-Allow-Origin: *&quot;to the server. But it was in my Arduino code all the time.</p>
  <p>I'm sorry, I'm not very good at this, so I'm asking for help. Where did I make a mistake?</p>