title: How to bypass CORS policy when sending get/post request from React JS?
body: <p>From my React JS app , I need to fetch data from servers in other domains.
  However, I am prevented by CORS policy and not able to fetch the data.</p>
  <p>Let us assume that my React app is running on localhost:3000 during the development.
  I want to make get/post call to another server running on  <a href="http://myserver.com" rel="nofollow noreferrer">http://myserver.com</a>
  The URL through which I want to fetch the data is <a href="http://ext-server.com/data/records?name=xyz" rel="nofollow noreferrer">http://ext-server.com/data/records?name=xyz</a></p>
  <p>I have installed http-proxy-middleware thru npm and using it in my react app.
  Created a setupProxy.js file under src folder with below content :</p>
  <pre><code>const { createProxyMiddleware} = require(&quot;http-proxy-middleware&quot;)
  module.exports = app =&gt; {
     app.use(
        createProxyMiddleware('/data/records' , {
          target:'http://ext-server.com',
          changeOrigin: true
       })
   )
  }
  </code></pre>
  <p>On the landing page of my react app (firstpage.js) when http://localhost:3000 is hit , I have added below piece of code to the button event that makes the get call to the <a href="http://ext-server.com" rel="nofollow noreferrer">http://ext-server.com</a></p>
  <pre><code>getTheData() {
    
  let url = &quot;http://ext-server.com/data/records?name=&quot; + encodeURIComponent(this.state.name); 
  
  axios.get(url,
    { 
      headers: {
       &quot;Content-Type&quot;:&quot;application/json;charset=UTL-8&quot;,
       &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;,
       Accept: &quot;application/json&quot;,
        }, 
        baseURL: 'http://ext-server.com'
     }
   ).then((response) =&gt; {
       console.log(response[&quot;access_token&quot;]);
    }).catch(error) =&gt; {
         console.log(&quot;Error: &quot;, error)
    }).then(function () {
         console.log(&quot;always call it&quot;)
  });
  
  }
  </code></pre>
  <p>In the package.json , I have added :</p>
  <pre><code>&quot;proxy&quot;: &quot;http://ext-server.com&quot;,
  &quot;homepage&quot;:&quot;http://localhost:3000&quot;,
  </code></pre>
  <p>But I am still getting below error:
  Access to XMLHttpRequest at 'http://ext-server.com/data/records?name= ' from origin 'http://localhost:3000' has been blocked by CORS policy.</p>
  <p>Is there anything that I am missing here ? what is the correct way to use this http-proxy-middleware?
  Any help will be very useful!
  Thanks</p>