简体   繁体   English

使用 Axios 从 React Js 向 url 发送请求时出现网络错误

[英]Network Error when sending request to url from React Js using Axios

I am trying to send a delete request using axios on my react js.我正在尝试在我的 react js 上使用 axios 发送删除请求。 I have deployed the node js server on heroku and using that as my link.我已经在 heroku 上部署了 node js 服务器并将其用作我的链接。 The following is the code in server.js which is deployed to heroku.以下是部署到heroku的server.js中的代码。

const deletePhoto = async (req, res) => {
  try {
    const file = bucket.file(req.params.name);
    file.delete();

    res.status(200).send("Photo deleted from bucket").end();
  } catch (err) {
    res
      .send(500)
      .send("Error occurred " + err)
      .end();
  }
};

The code below is my request which is sent using react.js and axios.下面的代码是我使用 react.js 和 axios 发送的请求。

const deleteImage = (docId) => {
    let url =
      "https://photobook-server-khushboo1028.herokuapp.com/files/" + docId;
    console.log(url);
    axios
      .delete(url)
      .then((response) => {
        console.log(response);

        alert("Successfully deleted image");
      })
      .catch((error) => {
        alert(error);
        console.log(error);
      });
  };

The error which I receive is this:我收到的错误是这样的:

Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:117)

The version of axios I am using is "0.23.0".我使用的 axios 版本是“0.23.0”。

I have even tried adding cors on my node.js server by doing app.use(cors) .我什至尝试通过执行app.use(cors)在我的 node.js 服务器上添加 cors。 Please help me out.请帮帮我。 Any suggestion is appreciated.任何建议表示赞赏。 Thank You谢谢你

  1. Not sure how you have set up your heroku server, does name in req.params corresponds to docId in your url?不确定你是如何设置你的 Heroku 服务器的,req.params 中的name是否与你的 url 中的docId相对应?
  2. Also can u check that the server on heroku is set up to use DELETE REST API request.您还可以检查 heroku 上的服务器是否设置为使用 DELETE REST API 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM