axios DELETE http请求被阻止

时间:2020-07-14 04:09:16

标签: javascript express http axios

我有一个react.js前端和node.js后端Web应用程序。我用mongodb作为数据库。

我使用axios从前端到后端发出HTTP请求。

我在发出DELETE请求时已经在 localhost 上测试了我的应用程序,没有问题。下面是我的HTTP流量浏览器控制台的屏幕截图。

enter image description here

下面是运行在“ 实时环境”(Netlify,Heroku,Mongo Atlas)中部署的此Web应用程序时浏览器控制台的另一个屏幕截图。

enter image description here

我不知道为什么HTTP DELETE请求被阻止?这是由于我未在mongodb地图集中配置的某种设置吗?

下面是我进行axios调用的前端的javascript代码

 AxiosInstance.get("/statistics")
          .then(function (response) {
            // handle success

            let message = "";
            let sortedResultArr = [];
            sortedResultArr = response.data.sort(function (a, b) {
              return a._id - b._id;
            });
            for (let i = 0; i < sortedResultArr.length; i++) {
              message +=
                " Day " +
                sortedResultArr[i]._id +
                " Profit : " +
                sortedResultArr[i].profitPerCup.toFixed(2);
            }
            AxiosInstance.delete("/statistics").then(function () {
              alert(message);
              window.location.reload();
            });
          })
          .catch(function (error) {
            // handle error
            console.log(error);
          });

我想知道是否由于我将一个axios调用嵌套在另一个axios调用中而发生了?

编辑:对于我的后端,我使用下面的代码部分来处理CORS

const corsOptions = {
  origin: [process.env.FRONTEND_URL, "http://localhost:3001"],
  credentials: true
};

app.use(cors(corsOptions));

我还想提到我也在不同的路由上进行了HTTP DELETE。对于我上面的屏幕截图,DELETE/statistics条路线被阻止了。但是,当应用程序在其他路由上进行HTTP DELETE调用时,响应正确,数据库操作也正确。

0 个答案:

没有答案