How to overcome the CORS issue in ReactJS

后端 未结 7 1828
失恋的感觉
失恋的感觉 2020-11-28 06:30

I am trying to make an API call through Axios in my React Application.However, Iam getting this CORS issue on my browser. I am wondering if i can resolve this issue from a c

相关标签:
7条回答
  • 2020-11-28 06:46

    You can have your React development server proxy your requests to that server. Simply send your requests to your local server like this: url: "/" And add the following line to your package.json file

    "proxy": "https://awww.api.com"
    

    Though if you are sending CORS requests to multiple sources, you'll have to manually configure the proxy yourself This link will help you set that up Create React App Proxying API requests

    0 讨论(0)
  • 2020-11-28 06:48

    In My Case adding chrome Cross Domain-CORS extension solved everything.You can try it hope it may help you....

    0 讨论(0)
  • 2020-11-28 06:51

    Another way besides @Nahush's answer, if you are already using Express framework in the project then you can avoid using Nginx for reverse-proxy.

    A simpler way is to use express-http-proxy

    1. run npm run build to create the bundle.

      var proxy = require('express-http-proxy');
      
      var app = require('express')();
      
      //define the path of build
      
      var staticFilesPath = path.resolve(__dirname, '..', 'build');
      
      app.use(express.static(staticFilesPath));
      
      app.use('/api/api-server', proxy('www.api-server.com'));
      

    Use "/api/api-server" from react code to call the API.

    So, that browser will send request to the same host which will be internally redirecting the request to another server and the browser will feel that It is coming from the same origin ;)

    0 讨论(0)
  • 2020-11-28 06:57

    the simplest way what I found from a tutorial of "TraversyMedia" is that just use https://cors-anywhere.herokuapp.com in 'axios' or 'fetch' api

    https://cors-anywhere.herokuapp.com/{type_your_url_here} 
    

    e.g.

    axios.get(`https://cors-anywhere.herokuapp.com/https://www.api.com/`)
    

    and in your case edit url as

    url: 'https://cors-anywhere.herokuapp.com/https://www.api.com',
    
    0 讨论(0)
  • 2020-11-28 06:58

    Temporary solve this issue by a chrome plugin called CORS. Btw backend server have to send proper header to front end requests.

    0 讨论(0)
  • 2020-11-28 06:58

    You can set up a express proxy server using http-proxy-middleware to bypass CORS:

    const express = require('express');
    const proxy = require('http-proxy-middleware');
    const path = require('path');
    const port = process.env.PORT || 8080;
    const app = express();
    
    app.use(express.static(__dirname));
    app.use('/proxy', proxy({
        pathRewrite: {
           '^/proxy/': '/'
        },
        target: 'https://server.com',
        secure: false
    }));
    
    app.get('*', (req, res) => {
       res.sendFile(path.resolve(__dirname, 'index.html'));
    });
    
    app.listen(port);
    console.log('Server started');
    

    From your react app all requests should be sent to /proxy endpoint and they will be redirected to the intended server.

    const URL = `/proxy/${PATH}`;
    return axios.get(URL);
    
    0 讨论(0)
提交回复
热议问题