title: React - PHP: How to fix problem of CORS with fetch request for POST request?
body: <p>I have problem with request POST in fetch function. I make REST API with react and PHP and I get error Access-Control-Allow-Origin is required. I have this header in my web api. This is my code (begin) in PHP:</p>
  
  <pre><code>header("Access-Control-Allow-Origin: *");
  header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
  header("Access-Control-Expose-Headers: access-control-allow-origin");
  header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  header("Content-Type: application/json; charset=UTF-8");
  </code></pre>
  
  <p>and in React:</p>
  
  <pre><code>//method = POST
  //body = {"name":"test","body":"test"}
  const apiCall = (url, method, body, resolve, reject) =&gt; {
      fetch(url, {
          method: method,
          headers: {
              'Content-Type': 'application/json; charset=utf-8'
          },
          body: JSON.stringify(body)
      }).then(resp =&gt; {
          if(resp.ok) {
              resp.json().then(json =&gt; resolve(json));
          }
          else {
              reject(resp);
          }
      });
  }
  </code></pre>
  
  <p>I try to communicate with other server and api - result was the same.
  Screen with error in Google Chrome browser:
  <a href="https://i.stack.imgur.com/H6tbV.png" rel="nofollow noreferrer">screen</a></p>
  
  <p>Please help.</p>