React: Axios Network Error

后端 未结 7 1614
既然无缘
既然无缘 2020-12-09 15:35

This is my first time using axios and I have encountered an error.

  axios.get(
    `http://someurl.com/page1?param1=1¶m2=${param2_id}`
  )
  .then(         


        
7条回答
  •  悲哀的现实
    2020-12-09 16:31

    I was having same issue on production on digital ocean droplet. I was using axios in ReactJS to call Node.js API.

    Although I included cors

    const cors = require('cors');
    app.use(cors());
    

    But I still had to add

    res.header( "Access-Control-Allow-Origin" );
    

    before calling out my controller. And it worked for me. There I realized that cors is not working properly. So I uninstalled and installed them again and It Works!

    Complete code is here.

    So either you use

     app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
      res.header("Access-Control-Allow-Headers", "x-access-token, Origin, X-Requested-With, Content-Type, Accept");
      next();
    });
    

    or use

    app.use(cors());
    

    It's the same.

提交回复
热议问题