小编Bun*_*nd3的帖子

如何修复来自 Axios 的随机 http 请求(404 响应)

HTTP 请求按预期工作,但我看到一个额外的请求/%3Canonymous%3E返回 404。这导致 Redux 中出现以下错误:

未处理的拒绝(TypeError):无法读取未定义的属性“数据”

我在其他组件中对其他路由的请求中没有看到 404,例如来自用户组件的 /api/users。我已更改获取请求和路由以匹配用户的请求和路由,但问题仍然存在。我已经在邮递员中尝试过该请求,它响应了预期的结果。仅当向浏览器中的订单资源(从应用程序)发出 get 请求时,才会发生对 /%3Canonymous%3E 的附加请求。

获取请求:

export const getOrders = () => dispatch => {
  axios
    .get("api/orders/")
    .then(res =>
      dispatch({
        type: GET_ORDERS,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};
Run Code Online (Sandbox Code Playgroud)

订购路线:

router.get(
  "/",
(req, res) => {
    Order.find()
      .then(orders => res.json(orders))
      .catch(err => {
        res.json(err);
      });
  }
);
Run Code Online (Sandbox Code Playgroud)

getOrder 减速器:

case GET_ORDERS:
      return {
        ...state,
        allOrders:
          action.payload
      };
Run Code Online (Sandbox Code Playgroud)

整个阶数缩减器:

import {
  GET_ORDERS,
  ADD_ORDER,
  EDIT_ORDER, …
Run Code Online (Sandbox Code Playgroud)

mongoose express reactjs redux axios

5
推荐指数
1
解决办法
2880
查看次数

标签 统计

axios ×1

express ×1

mongoose ×1

reactjs ×1

redux ×1