【问题标题】:Using Express and Axios, Axios will not recognize PUT Requests使用 Express 和 Axios,Axios 将无法识别 PUT 请求
【发布时间】:2021-12-11 05:20:40
【问题描述】:

我已将我的用例简化为一个简单的测试,其中我单击一个按钮并使用 axios API 发送一个 UPDATE put 请求。 其他三种请求方法——发布、获取和删除,都可以正常工作并被识别。但是,我的 PUT 方法给出了 404 Not Found 错误,就好像我没有用 express 建立方法一样我的服务器。

这是请求的代码,由该事件处理程序(按钮)触发:

const handleUpdate = async (e, id) => {
        e.stopPropagation();
        // navigate(`/restaurants/${id}/update`);
        //update test
        try {
            const updatedRestaurant = await RestaurantFinder.put(`/${id}`, {
                name: "taco bell",
                location: "dogtown",
                price_range: "2"
              });
              console.log(updatedRestaurant);
              navigate("/");
        } catch(err) {
            console.log(err);
        }
      };

这是api的实例化:

import axios from "axios";

export default axios.create({
    baseURL: "http://localhost:3001/api/v1/restaurants"
    }
);

这是 Express 中的请求。注意第一个日志“这存在吗???”从不显示。永远找不到“http://localhost:3001/api/v1/restaurants/id”的put请求地址。

//UPDATE a restaurant
app.put("/api/v1/restaurants/:id"), async (req, res) => {
    console.log("does this exist???");
    try {
        const results = await db.query(
          "UPDATE restaurants SET name = $1, location = $2, price_range = $3 where id = $4 returning *",
          [req.body.name, req.body.location, req.body.price_range, req.params.id]
        );
    
        res.status(200).json({
          status: "succes",
          data: {
            restaurant: results.rows[0],
          },
        });
      } catch (err) {
        console.log(err);
      }
      console.log(req.params.id);
      console.log(req.body);
};

我已经仔细研究了 StackOverflow 以获得答案,我想我目前已经查看了 50 多个帖子。这本应该如此简单,但我找不到一个答案,也找不到其他人发生的情况。

如果有人可以帮助我解决问题所在,我将不胜感激!

编辑 1 @Stephen:

const app = express();
app.use(cors(
  {
  methods: ["POST", "GET", "DELETE", "PUT"]
}
));
app.use(express.json());

所以我没有运气就把它改成了这个。本来我只是用cors(),我的理解是默认允许put方法。

【问题讨论】:

    标签: node.js postgresql express axios request


    【解决方案1】:

    您的服务器是否允许put 方法?也许您的服务器只允许 access-control-allow-methods 字段中的其他服务器。

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods

    对于通过 http 进行测试,请确保这两个都由您的服务器设置。

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
    

    【讨论】:

    • 感谢您的回复。我会立即检查并回复您!
    • 从您的开发控制台网络调用中发布请求和响应标头。划掉你的 cookie。
    • 我现在就去做。我刚刚通过编辑我的帖子来回应你。这是我的第一篇 stackoverflow 帖子,我希望我做得对...
    • @outoftheblue,您的控制台显示什么?没有响应、挂起或错误消息?
    • 在每次更新提示时,它会发送两个请求:请求 URL:localhost:3001/api/v1/restaurants/6 请求方法:OPTIONS 状态代码:204 无内容远程地址:[::1]:3001 推荐人策略:strict-origin -when-cross-origin AND 请求 URL:localhost:3001/api/v1/restaurants/6 请求方法:PUT 状态代码:404 Not Found 远程地址:[::1]:3001 推荐人策略:strict-origin-when-cross-origin
    猜你喜欢
    • 2021-09-24
    • 2019-05-20
    • 2022-12-21
    • 2023-03-12
    • 2016-09-29
    • 2020-01-12
    • 2018-05-20
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多