title: get data from wp rest api from a cross domain
body: <p>I tried to get data from wordpress rest api from a js application which is in a cross domain, I got <strong>Access-Control-Allow-Origin</strong> error</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>axios.get(`https://bikeguy.xyz/wp-json/wp/v2/posts?categories=86`)
     .then( posts =&gt; console.log(posts) )</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://unpkg.com/axios/dist/axios.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://i.stack.imgur.com/POF3V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/POF3V.png" alt="enter image description here"></a></p>
  
  <p>To allow cross domain access, I put these codes in <strong>functions.php</strong> file , but I get still same errors</p>
  
  <pre><code>// Hook.
  add_action( 'rest_api_init', 'wp_rest_allow_all_cors', 15 );
  /**
   * Allow all CORS.
   *
   * @since 1.0.0
   */
  function wp_rest_allow_all_cors() {
      // Remove the default filter.
      remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
      // Add a Custom filter.
      add_filter( 'rest_pre_serve_request', function( $value ) {
          header( 'Access-Control-Allow-Origin: *' );
          header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
          header( 'Access-Control-Allow-Credentials: true' );
          return $value;
      });
  } // End fucntion wp_rest_allow_all_cors().
  </code></pre>