小编Abh*_*bhi的帖子

如何修复 axios Next js 中的“错误:请求失败,状态代码 404”

当尝试在 getInitialProps 中调用 API 端点(调度 redux 操作)时,我正在使用 next js。我收到 404 错误,我的 /api/ 在 nginx 服务器中被代理,所有其他路由都工作得很好,只有这个路由导致了问题。

我尝试将 api fetch 函数调用更改为异步,但仍然出现相同的错误。

static async getInitialProps({ store, query: { id } }) {
    await store.dispatch(fetchP(id));
    console.log('GetIntial');
    return { id };
 }
Run Code Online (Sandbox Code Playgroud)

我的动作创造者

export const fetchp = id => async dispatch => {
  dispatch({ type: FETCH_P_DETAILS_STARTED });
  await API.get(`ps/${id}`)
    .then(res => {
      console.log(res);
      dispatch({
        type: FETCH_P_DETAILS_SUCCESS,
        payload: res.data.data,
      });
    })
    .catch(err => {
      console.log(err);
      if (!err.response) {
        // network error
        err.message = 'Error: Network Error'; …
Run Code Online (Sandbox Code Playgroud)

javascript nginx reactjs axios next.js

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

标签 统计

axios ×1

javascript ×1

next.js ×1

nginx ×1

reactjs ×1