Cannot send json to express server from the browser : <p>I established 2 servers: the first one is for frontend (localhost:7200), the second one is for backend (localhost:7300). And there is a test request made by frontend to backend, correspondingly, on a route '/test'.</p>

<p>The problem is when I send a json object (when 'btn' button is clicked) it rejects with the following message:</p>

<blockquote>
  <p>Access to XMLHttpRequest at '<a href="http://localhost:7300/test" rel="nofollow noreferrer">http://localhost:7300/test</a>' from origin '<a href="http://localhost:7200" rel="nofollow noreferrer">http://localhost:7200</a>' 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.</p>
</blockquote>

<p>Of course, I  set 'Access-Control-Allow-Origin' header on backend (see below).</p>

<p>This happens if I set 'Content-Type' header as 'application/json'. But it works fine if there is no header set at all or the header is 'application/x-www-form-urlencoded' (and, therefore, the message sent is not JSON-like).</p>

<p>The code on frontend</p>

<pre><code>btn.addEventListener('click', () =&gt; {
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://localhost:7300/test', true);
    xhr.setRequestHeader('Accept', 'application/json');
    xhr.setRequestHeader('Content-Type', 'application/json');

    xhr.addEventListener('load', (e) =&gt; {
      console.log(e.target.responseText);
    });

    xhr.send(JSON.stringify({name: 'kek'}))
});
</code></pre>

<p>The code on backend:</p>

<pre><code>const express = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json());

app.post('/test', (req, res) =&gt; {
  res.set('Access-Control-Allow-Origin', '*');
  res.set('Access-Control-Allow-Headers', 'Content-Type')
  // res.set('Content-Type', 'application/json');
  res.header("Content-Type",'application/json');
  console.log(req.body)
  res.json({success: true});
});
</code></pre>
